Skip to content

Commit

Permalink
feat: add trait impls for OsStr/Path compatibility (#5)
Browse files Browse the repository at this point in the history
also restore codecov token for ci code coverage reporting
  • Loading branch information
polazarus authored Aug 18, 2023
1 parent 54b9941 commit 980ec14
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ jobs:
uses: codecov/codecov-action@v3
with:
file: codecov.json
fail_ci_if_error: true
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
81 changes: 81 additions & 0 deletions src/string/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,66 @@ where
}
}

impl<'borrow, B> PartialEq<std::ffi::OsStr> for HipStr<'borrow, B>
where
B: Backend,
{
#[inline]
fn eq(&self, other: &std::ffi::OsStr) -> bool {
self.as_str().eq(other)
}
}

impl<'borrow, B> PartialEq<HipStr<'borrow, B>> for std::ffi::OsStr
where
B: Backend,
{
#[inline]
fn eq(&self, other: &HipStr<'borrow, B>) -> bool {
self.eq(other.as_str())
}
}

impl<'a, 'borrow, B> PartialEq<&'a std::ffi::OsStr> for HipStr<'borrow, B>
where
B: Backend,
{
#[inline]
fn eq(&self, other: &&'a std::ffi::OsStr) -> bool {
self.as_str().eq(*other)
}
}

impl<'a, 'borrow, B> PartialEq<HipStr<'borrow, B>> for &'a std::ffi::OsStr
where
B: Backend,
{
#[inline]
fn eq(&self, other: &HipStr<'borrow, B>) -> bool {
(*self).eq(other.as_str())
}
}

impl<'borrow, B> PartialEq<std::ffi::OsString> for HipStr<'borrow, B>
where
B: Backend,
{
#[inline]
fn eq(&self, other: &std::ffi::OsString) -> bool {
self.as_str().eq(other)
}
}

impl<'borrow, B> PartialEq<HipStr<'borrow, B>> for std::ffi::OsString
where
B: Backend,
{
#[inline]
fn eq(&self, other: &HipStr<'borrow, B>) -> bool {
self.eq(other.as_str())
}
}

impl<'borrow, B> Ord for HipStr<'borrow, B>
where
B: Backend,
Expand Down Expand Up @@ -171,6 +231,27 @@ mod tests {
assert_eq!(c, h);
}

#[test]
fn test_eq_os_str() {
let s = "abc";
let os: &std::ffi::OsStr = s.as_ref();
let h = HipStr::from(s);
assert_eq!(h, os);
assert_eq!(os, h);
assert_eq!(&h, os);
assert_eq!(os, &h);
}

#[test]
fn test_eq_os_string() {
let s = "abc";
let os: &std::ffi::OsStr = s.as_ref();
let oss = os.to_os_string();
let h = HipStr::from(s);
assert_eq!(h, oss);
assert_eq!(oss, h);
}

#[test]
fn test_ord() {
let h1 = HipStr::borrowed("abc");
Expand Down
92 changes: 92 additions & 0 deletions src/string/convert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Conversion trait implementations for `HipStr`.

use std::borrow::Cow;
use std::net::ToSocketAddrs;

use super::HipStr;
use crate::bytes::HipByt;
Expand All @@ -16,6 +17,34 @@ where
}
}

impl<'borrow, B> AsRef<[u8]> for HipStr<'borrow, B>
where
B: Backend,
{
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

impl<'borrow, B> AsRef<std::ffi::OsStr> for HipStr<'borrow, B>
where
B: Backend,
{
fn as_ref(&self) -> &std::ffi::OsStr {
self.as_str().as_ref()
}
}

impl<'borrow, B> AsRef<std::path::Path> for HipStr<'borrow, B>
where
B: Backend,
{
fn as_ref(&self) -> &std::path::Path {
self.as_str().as_ref()
}
}

// Infallible conversions

impl<'borrow, B> From<&str> for HipStr<'borrow, B>
Expand Down Expand Up @@ -73,6 +102,19 @@ where
}
}

impl<'borrow, B> From<HipStr<'borrow, B>> for std::ffi::OsString
where
B: Backend,
{
#[inline]
fn from(value: HipStr<B>) -> Self {
value
.into_string()
.unwrap_or_else(|value| value.as_str().into())
.into()
}
}

impl<'borrow, B> From<HipStr<'borrow, B>> for HipByt<'borrow, B>
where
B: Backend,
Expand Down Expand Up @@ -144,9 +186,21 @@ where
}
}

impl<'borrow, B> ToSocketAddrs for HipStr<'borrow, B>
where
B: Backend,
{
type Iter = <str as ToSocketAddrs>::Iter;

fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
self.as_str().to_socket_addrs()
}
}

#[cfg(test)]
mod tests {
use std::borrow::Cow;
use std::net::ToSocketAddrs;

use crate::{HipByt, HipStr};

Expand Down Expand Up @@ -203,6 +257,13 @@ mod tests {
assert_eq!(v.as_str(), "abc");
}

#[test]
fn into_os_string() {
let h = HipStr::from("abc");
let os_string: std::ffi::OsString = h.into();
assert_eq!(os_string, "abc");
}

#[test]
fn test_into_hipbyt() {
let v = "a".repeat(42); // string's length > inline capacity
Expand Down Expand Up @@ -267,4 +328,35 @@ mod tests {
assert!(HipStr::try_from(&hb).is_err());
assert!(HipStr::try_from(hb).is_err());
}

#[test]
fn as_ref_bytes() {
let h = HipStr::from("abc");
let b: &[u8] = h.as_ref();
assert!(std::ptr::eq(h.as_bytes(), b));
}

#[test]
fn as_ref_os_str() {
let h = HipStr::from("abc");
let o: &std::ffi::OsStr = h.as_ref();
assert_eq!(o, "abc");
assert!(std::ptr::eq(h.as_str().as_ref(), o));
}

#[test]
fn as_ref_path() {
let h = HipStr::from("abc");
let p: &std::path::Path = h.as_ref();
assert_eq!(p, std::path::Path::new("abc"));
assert!(std::ptr::eq(h.as_str().as_ref(), p));
}

#[test]
fn to_sock_addrs() {
let h = HipStr::from("0.0.0.0:80");
let v: Vec<_> = h.to_socket_addrs().unwrap().collect();
let v2: Vec<_> = "0.0.0.0:80".to_socket_addrs().unwrap().collect();
assert_eq!(v, v2);
}
}

0 comments on commit 980ec14

Please sign in to comment.