Skip to content

Commit

Permalink
Allow file to be borrowed
Browse files Browse the repository at this point in the history
  • Loading branch information
StratusFearMe21 committed Dec 8, 2023
1 parent 508b6f1 commit 8196a96
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions symphonia-core/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ impl MediaSource for std::fs::File {
}
}

impl<'a> MediaSource for &'a mut std::fs::File {
/// Returns if the `std::io::File` backing the `MediaSource` is seekable.
///
/// Note: This operation involves querying the underlying file descriptor for information and
/// may be moderately expensive. Therefore it is recommended to cache this value if used often.
fn is_seekable(&self) -> bool {
// If the file's metadata is available, and the file is a regular file (i.e., not a FIFO,
// etc.), then the MediaSource will be seekable. Otherwise assume it is not. Note that
// metadata() follows symlinks.
match self.metadata() {
Ok(metadata) => metadata.is_file(),
_ => false,
}
}

/// Returns the length in bytes of the `std::io::File` backing the `MediaSource`.
///
/// Note: This operation involves querying the underlying file descriptor for information and
/// may be moderately expensive. Therefore it is recommended to cache this value if used often.
fn byte_len(&self) -> Option<u64> {
match self.metadata() {
Ok(metadata) => Some(metadata.len()),
_ => None,
}
}
}

impl<T: std::convert::AsRef<[u8]> + Send + Sync> MediaSource for io::Cursor<T> {
/// Always returns true since a `io::Cursor<u8>` is always seekable.
fn is_seekable(&self) -> bool {
Expand Down

0 comments on commit 8196a96

Please sign in to comment.