Skip to content

Commit 096c24c

Browse files
committed
Rename from_file to from_file_with_path and add from_file with no path.
1 parent 08bb900 commit 096c24c

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/body.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,30 @@ impl Body {
372372
{
373373
let path = path.as_ref();
374374
let file = async_std::fs::File::open(path).await?;
375-
Self::from_file(file, path).await
375+
Self::from_file_with_path(file, path).await
376+
}
377+
378+
/// Create a `Body` from an already-open file.
379+
///
380+
/// The Mime type is sniffed from the file contents if possible, otherwise
381+
/// is set to `application/octet-stream`.
382+
///
383+
/// # Examples
384+
///
385+
/// ```no_run
386+
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
387+
/// use http_types::{Body, Response, StatusCode};
388+
///
389+
/// let mut res = Response::new(StatusCode::Ok);
390+
/// let path = std::path::Path::new("/path/to/file");
391+
/// let file = async_std::fs::File::open(path).await?;
392+
/// res.set_body(Body::from_file(file).await?);
393+
/// # Ok(()) }) }
394+
/// ```
395+
#[cfg(all(feature = "fs", not(target_os = "unknown")))]
396+
#[inline]
397+
pub async fn from_file(file: async_std::fs::File) -> io::Result<Self> {
398+
Self::from_file_with_path(file, std::path::Path::new("")).await
376399
}
377400

378401
/// Create a `Body` from an already-open file.
@@ -393,11 +416,11 @@ impl Body {
393416
/// let mut res = Response::new(StatusCode::Ok);
394417
/// let path = std::path::Path::new("/path/to/file");
395418
/// let file = async_std::fs::File::open(path).await?;
396-
/// res.set_body(Body::from_file(file, path).await?);
419+
/// res.set_body(Body::from_file_with_path(file, path).await?);
397420
/// # Ok(()) }) }
398421
/// ```
399422
#[cfg(all(feature = "fs", not(target_os = "unknown")))]
400-
pub async fn from_file(
423+
pub async fn from_file_with_path(
401424
mut file: async_std::fs::File,
402425
path: &std::path::Path,
403426
) -> io::Result<Self> {

0 commit comments

Comments
 (0)