Skip to content

Commit 672e9cb

Browse files
committed
Make ByteStr/ByteString a more-opaque wrapper like OsStr/OsString
1 parent 055d0d6 commit 672e9cb

File tree

13 files changed

+637
-94
lines changed

13 files changed

+637
-94
lines changed

library/alloc/src/bstr.rs

Lines changed: 338 additions & 43 deletions
Large diffs are not rendered by default.

library/alloc/src/slice.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![stable(feature = "rust1", since = "1.0.0")]
1111

1212
use core::borrow::{Borrow, BorrowMut};
13+
use core::bstr::ByteStr;
1314
#[cfg(not(no_global_oom_handling))]
1415
use core::clone::TrivialClone;
1516
#[cfg(not(no_global_oom_handling))]
@@ -661,6 +662,16 @@ impl [u8] {
661662
me.make_ascii_lowercase();
662663
me
663664
}
665+
666+
/// Converts a `Box<[u8]>` into a `Box<ByteStr>` without copying or allocating.
667+
#[cfg(not(no_global_oom_handling))]
668+
#[rustc_allow_incoherent_impl]
669+
#[unstable(feature = "bstr", issue = "134915")]
670+
#[inline]
671+
pub fn into_boxed_byte_str(self: Box<[u8]>) -> Box<ByteStr> {
672+
// SAFETY: `ByteStr` is a thin wrapper over `[u8]`
673+
unsafe { Box::from_raw(Box::into_raw(self) as _) }
674+
}
664675
}
665676

666677
////////////////////////////////////////////////////////////////////////////////

library/alloc/src/vec/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ pub use self::extract_if::ExtractIf;
9393
use crate::alloc::{Allocator, Global};
9494
use crate::borrow::{Cow, ToOwned};
9595
use crate::boxed::Box;
96+
#[cfg(not(no_global_oom_handling))]
97+
use crate::bstr::ByteString;
9698
use crate::collections::TryReserveError;
9799
use crate::raw_vec::RawVec;
98100

@@ -3350,6 +3352,17 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
33503352
}
33513353
}
33523354

3355+
#[cfg(not(no_global_oom_handling))]
3356+
impl Vec<u8> {
3357+
/// Converts a vector of bytes into a byte string.
3358+
#[unstable(feature = "bstr", issue = "134915")]
3359+
#[inline]
3360+
#[rustc_const_unstable(feature = "bstr", issue = "134915")]
3361+
pub const fn into_byte_string(self) -> ByteString {
3362+
ByteString(self)
3363+
}
3364+
}
3365+
33533366
impl<T, A: Allocator, const N: usize> Vec<[T; N], A> {
33543367
/// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`.
33553368
///

0 commit comments

Comments
 (0)