Skip to content

Commit

Permalink
feat: add into cow conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
polazarus committed Jun 22, 2024
1 parent 999696c commit 35616c8
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/bytes/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ where
}
}

impl<'borrow, B> From<HipByt<'borrow, B>> for Cow<'borrow, [u8]>
where
B: Backend,
{
#[inline]
fn from(value: HipByt<'borrow, B>) -> Self {
value
.into_borrowed()
.map_or_else(|value| Cow::Owned(value.into()), Cow::Borrowed)
}
}

// Fallible conversions

// => none for now
Expand Down Expand Up @@ -150,4 +162,15 @@ mod tests {
let v: Vec<_> = a.into();
assert_eq!(v.as_slice(), &arr);
}

#[test]
fn test_into_cow() {
let h = HipByt::from_static(b"abc");
let c: Cow<'static, [u8]> = h.into();
assert_eq!(c, Cow::Borrowed(b"abc"));

let h = HipByt::from(b"abc");
let c: Cow<'static, [u8]> = h.into();
assert_eq!(c, Cow::<'static, [u8]>::Owned(b"abc".into()));
}
}
23 changes: 23 additions & 0 deletions src/os_string/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ where
}
}

impl<'borrow, B> From<HipOsStr<'borrow, B>> for Cow<'borrow, OsStr>
where
B: Backend,
{
#[inline]
fn from(value: HipOsStr<'borrow, B>) -> Self {
value
.into_borrowed()
.map_or_else(|value| Cow::Owned(value.into()), Cow::Borrowed)
}
}

// Fallible conversions

//?
Expand Down Expand Up @@ -258,6 +270,17 @@ mod tests {
assert_eq!(v, "abc");
}

#[test]
fn test_into_cow() {
let h = HipOsStr::from_static("abc");
let c: Cow<'static, OsStr> = h.into();
assert_eq!(c, Cow::Borrowed("abc"));

let h = HipOsStr::from("abc");
let c: Cow<'static, OsStr> = h.into();
assert_eq!(c, Cow::<'static, OsStr>::Owned("abc".into()));
}

#[test]
fn test_into_hipbyt() {
let v = "a".repeat(42); // string's length > inline capacity
Expand Down
23 changes: 23 additions & 0 deletions src/path/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ where
}
}

impl<'borrow, B> From<HipPath<'borrow, B>> for Cow<'borrow, Path>
where
B: Backend,
{
#[inline]
fn from(value: HipPath<'borrow, B>) -> Self {
value
.into_borrowed()
.map_or_else(|value| Cow::Owned(value.into()), Cow::Borrowed)
}
}

// Fallible conversions

//?
Expand Down Expand Up @@ -401,6 +413,17 @@ mod tests {
assert_eq!(v, Path::new("abc"));
}

#[test]
fn test_into_cow() {
let h = HipPath::from_static("abc");
let c: Cow<'static, Path> = h.into();
assert_eq!(c, Cow::Borrowed(Path::new("abc")));

let h = HipPath::from("abc");
let c: Cow<'static, Path> = h.into();
assert_eq!(c, Cow::<'static, Path>::Owned("abc".into()));
}

#[test]
fn test_into_os_string() {
let v = "a".repeat(42); // string's length > inline capacity
Expand Down
23 changes: 23 additions & 0 deletions src/string/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ where
}
}

impl<'borrow, B> From<HipStr<'borrow, B>> for Cow<'borrow, str>
where
B: Backend,
{
#[inline]
fn from(value: HipStr<'borrow, B>) -> Self {
value
.into_borrowed()
.map_or_else(|value| Cow::Owned(value.into()), Cow::Borrowed)
}
}

// Fallible conversions

impl<'borrow, B> TryFrom<HipByt<'borrow, B>> for HipStr<'borrow, B>
Expand Down Expand Up @@ -270,6 +282,17 @@ mod tests {
assert_eq!(v.as_str(), "abc");
}

#[test]
fn test_into_cow() {
let h = HipStr::from_static("abc");
let c: Cow<'static, str> = h.into();
assert_eq!(c, Cow::Borrowed("abc"));

let h = HipStr::from("abc");
let c: Cow<'static, str> = h.into();
assert_eq!(c, Cow::<'static, str>::Owned("abc".into()));
}

#[test]
#[cfg(feature = "std")]
fn into_os_string() {
Expand Down

0 comments on commit 35616c8

Please sign in to comment.