-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move OsStringExt
and OsStrExt
to std::os
#84967
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::ffi::{OsStr, OsString}; | ||
use crate::mem; | ||
use crate::sealed::Sealed; | ||
use crate::sys::os_str::Buf; | ||
use crate::sys_common::{AsInner, FromInner, IntoInner}; | ||
|
||
// Note: this file is currently reused in other `std::os::{platform}::ffi` modules to reduce duplication. | ||
// Keep this in mind when applying changes to this file that only apply to `unix`. | ||
|
||
/// Platform-specific extensions to [`OsString`]. | ||
/// | ||
/// This trait is sealed: it cannot be implemented outside the standard library. | ||
/// This is so that future additional methods are not breaking changes. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait OsStringExt: Sealed { | ||
/// Creates an [`OsString`] from a byte vector. | ||
/// | ||
/// See the module documentation for an example. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn from_vec(vec: Vec<u8>) -> Self; | ||
|
||
/// Yields the underlying byte vector of this [`OsString`]. | ||
/// | ||
/// See the module documentation for an example. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn into_vec(self) -> Vec<u8>; | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl OsStringExt for OsString { | ||
fn from_vec(vec: Vec<u8>) -> OsString { | ||
FromInner::from_inner(Buf { inner: vec }) | ||
} | ||
fn into_vec(self) -> Vec<u8> { | ||
self.into_inner().inner | ||
} | ||
} | ||
|
||
/// Platform-specific extensions to [`OsStr`]. | ||
/// | ||
/// This trait is sealed: it cannot be implemented outside the standard library. | ||
/// This is so that future additional methods are not breaking changes. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait OsStrExt: Sealed { | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
/// Creates an [`OsStr`] from a byte slice. | ||
/// | ||
/// See the module documentation for an example. | ||
fn from_bytes(slice: &[u8]) -> &Self; | ||
|
||
/// Gets the underlying byte view of the [`OsStr`] slice. | ||
/// | ||
/// See the module documentation for an example. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn as_bytes(&self) -> &[u8]; | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl OsStrExt for OsStr { | ||
#[inline] | ||
fn from_bytes(slice: &[u8]) -> &OsStr { | ||
unsafe { mem::transmute(slice) } | ||
} | ||
#[inline] | ||
fn as_bytes(&self) -> &[u8] { | ||
&self.as_inner().inner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,30 +2,25 @@ | |
//! systems: just a `Vec<u8>`/`[u8]`. | ||
|
||
use crate::borrow::Cow; | ||
use crate::ffi::{OsStr, OsString}; | ||
|
||
use crate::fmt; | ||
use crate::mem; | ||
use crate::rc::Rc; | ||
use crate::sealed::Sealed; | ||
use crate::str; | ||
use crate::sync::Arc; | ||
use crate::sys_common::bytestring::debug_fmt_bytestring; | ||
use crate::sys_common::{AsInner, FromInner, IntoInner}; | ||
use crate::sys_common::{AsInner, IntoInner}; | ||
|
||
use core::str::lossy::Utf8Lossy; | ||
|
||
#[derive(Hash)] | ||
pub(crate) struct Buf { | ||
#[repr(transparent)] | ||
pub struct Buf { | ||
pub inner: Vec<u8>, | ||
} | ||
|
||
// FIXME: | ||
// `Buf::as_slice` current implementation relies | ||
// on `Slice` being layout-compatible with `[u8]`. | ||
// When attribute privacy is implemented, `Slice` should be annotated as `#[repr(transparent)]`. | ||
// Anyway, `Slice` representation and layout are considered implementation detail, are | ||
// not documented and must not be relied upon. | ||
pub(crate) struct Slice { | ||
#[repr(transparent)] | ||
pub struct Slice { | ||
Comment on lines
-22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
pub inner: [u8], | ||
} | ||
|
||
|
@@ -243,63 +238,3 @@ impl Slice { | |
self.inner.eq_ignore_ascii_case(&other.inner) | ||
} | ||
} | ||
|
||
/// Platform-specific extensions to [`OsString`]. | ||
/// | ||
/// This trait is sealed: it cannot be implemented outside the standard library. | ||
/// This is so that future additional methods are not breaking changes. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait OsStringExt: Sealed { | ||
/// Creates an [`OsString`] from a byte vector. | ||
/// | ||
/// See the module documentation for an example. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn from_vec(vec: Vec<u8>) -> Self; | ||
|
||
/// Yields the underlying byte vector of this [`OsString`]. | ||
/// | ||
/// See the module documentation for an example. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn into_vec(self) -> Vec<u8>; | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl OsStringExt for OsString { | ||
fn from_vec(vec: Vec<u8>) -> OsString { | ||
FromInner::from_inner(Buf { inner: vec }) | ||
} | ||
fn into_vec(self) -> Vec<u8> { | ||
self.into_inner().inner | ||
} | ||
} | ||
|
||
/// Platform-specific extensions to [`OsStr`]. | ||
/// | ||
/// This trait is sealed: it cannot be implemented outside the standard library. | ||
/// This is so that future additional methods are not breaking changes. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait OsStrExt: Sealed { | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
/// Creates an [`OsStr`] from a byte slice. | ||
/// | ||
/// See the module documentation for an example. | ||
fn from_bytes(slice: &[u8]) -> &Self; | ||
|
||
/// Gets the underlying byte view of the [`OsStr`] slice. | ||
/// | ||
/// See the module documentation for an example. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn as_bytes(&self) -> &[u8]; | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl OsStrExt for OsStr { | ||
#[inline] | ||
fn from_bytes(slice: &[u8]) -> &OsStr { | ||
unsafe { mem::transmute(slice) } | ||
} | ||
#[inline] | ||
fn as_bytes(&self) -> &[u8] { | ||
&self.as_inner().inner | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as outdated.
Sorry, something went wrong.