Skip to content

Commit

Permalink
Add IO conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dylni committed Nov 11, 2023
1 parent 44e265a commit 054211d
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 178 deletions.
17 changes: 2 additions & 15 deletions src/common/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,8 @@ use std::ffi::OsStr;
use std::ffi::OsString;
use std::result;

#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
use std::os::fortanix_sgx as os;
#[cfg(target_os = "hermit")]
use std::os::hermit as os;
#[cfg(target_os = "solid_asp3")]
use std::os::solid as os;
#[cfg(unix)]
use std::os::unix as os;
#[cfg(target_os = "wasi")]
use std::os::wasi as os;
#[cfg(target_os = "xous")]
use std::os::xous as os;

use os::ffi::OsStrExt;
use os::ffi::OsStringExt;
use super::os::ffi::OsStrExt;
use super::os::ffi::OsStringExt;

pub(crate) type EncodingError = Infallible;

Expand Down
30 changes: 30 additions & 0 deletions src/common/convert_io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::borrow::Cow;
use std::ffi::OsStr;
use std::ffi::OsString;

use super::os::ffi::OsStrExt;
use super::os::ffi::OsStringExt;

pub(crate) fn os_str_from_bytes(string: &[u8]) -> Option<&OsStr> {
Some(OsStr::from_bytes(string))
}

pub(crate) fn os_str_to_bytes(string: &OsStr) -> Option<&'_ [u8]> {
Some(string.as_bytes())
}

pub(crate) fn os_str_to_bytes_lossy(string: &OsStr) -> Cow<'_, [u8]> {
Cow::Borrowed(string.as_bytes())
}

pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Option<OsString> {
Some(OsString::from_vec(string))
}

pub(crate) fn os_string_into_vec(string: OsString) -> Option<Vec<u8>> {
Some(string.into_vec())
}

pub(crate) fn os_string_into_vec_lossy(string: OsString) -> Vec<u8> {
string.into_vec()
}
15 changes: 15 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
use std::os::fortanix_sgx as os;
#[cfg(target_os = "hermit")]
use std::os::hermit as os;
#[cfg(target_os = "solid_asp3")]
use std::os::solid as os;
#[cfg(unix)]
use std::os::unix as os;
#[cfg(target_os = "wasi")]
use std::os::wasi as os;
#[cfg(target_os = "xous")]
use std::os::xous as os;

pub(super) mod convert_io;

if_conversions! {
pub(super) mod convert;

Expand Down
11 changes: 1 addition & 10 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ use super::iter::Utf8Chunks;
use super::pattern::Encoded as EncodedPattern;
use super::util;
use super::util::MAX_UTF8_LENGTH;
use super::OsStrBytes;
use super::Pattern;

if_conversions! {
use super::imp::raw;
use super::OsStrBytes;
}

#[cfg(not(feature = "conversions"))]
use super::private;

fn is_boundary(string: &OsStr, index: usize) -> bool {
let string = string.as_encoded_bytes();
debug_assert!(index < string.len());
Expand Down Expand Up @@ -134,12 +131,6 @@ where
trim_matches(string, pat, OsStrBytesExt::strip_prefix)
}

#[cfg(not(feature = "conversions"))]
trait OsStrBytes: private::Sealed {}

#[cfg(not(feature = "conversions"))]
impl OsStrBytes for OsStr {}

/// An extension trait providing additional methods to [`OsStr`].
///
/// In most cases, this trait will prevent needing to call
Expand Down
Loading

0 comments on commit 054211d

Please sign in to comment.