Skip to content

Commit

Permalink
slice: add path and pathbuf impls
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed Dec 4, 2024
1 parent e506920 commit 3f15b17
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ mod slice_arc;
#[cfg(feature = "bytes")]
mod slice_bytes;

use std::sync::Arc;
use std::{
path::{Path, PathBuf},
sync::Arc,
};

#[cfg(not(feature = "bytes"))]
pub use slice_arc::Slice;
Expand Down Expand Up @@ -45,6 +48,18 @@ impl From<&String> for Slice {
}
}

impl From<&Path> for Slice {
fn from(value: &Path) -> Self {
Self::from(value.as_os_str().as_encoded_bytes())
}
}

impl From<PathBuf> for Slice {
fn from(value: PathBuf) -> Self {
Self::from(value.as_os_str().as_encoded_bytes())
}
}

impl From<Arc<str>> for Slice {
fn from(value: Arc<str>) -> Self {
Self::from(&*value)
Expand Down Expand Up @@ -161,6 +176,7 @@ mod serde {
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::Slice;
use std::{fmt::Debug, sync::Arc};
Expand Down Expand Up @@ -204,7 +220,7 @@ mod tests {

// - io::Read
let reader = std::io::Cursor::new(vec![1, 2, 3, 4]);
let slice = Slice::from_reader(&mut reader.clone(), 4).expect("read");
let slice = Slice::from_reader(&mut reader, 4).expect("read");
assert_eq!(slice, vec![1, 2, 3, 4]);
}
}

0 comments on commit 3f15b17

Please sign in to comment.