Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
710 changes: 0 additions & 710 deletions library/alloc/src/bstr.rs

This file was deleted.

1,005 changes: 1,005 additions & 0 deletions library/alloc/src/byte_str.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
#![feature(async_fn_traits)]
#![feature(async_iterator)]
#![feature(borrowed_buf_init)]
#![feature(bstr)]
#![feature(bstr_internals)]
#![feature(byte_str)]
#![feature(byte_str_internals)]
#![feature(can_vector)]
#![feature(case_ignorable)]
#![feature(cast_maybe_uninit)]
Expand Down Expand Up @@ -246,8 +246,8 @@ pub mod alloc;
// to allow code to have `use boxed::Box;` declarations.
pub mod borrow;
pub mod boxed;
#[unstable(feature = "bstr", issue = "134915")]
pub mod bstr;
#[unstable(feature = "byte_str", issue = "134915")]
pub mod byte_str;
pub mod collections;
#[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
pub mod ffi;
Expand Down
12 changes: 12 additions & 0 deletions library/alloc/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use core::borrow::{Borrow, BorrowMut};
#[cfg(not(no_global_oom_handling))]
use core::byte_str::ByteStr;
#[cfg(not(no_global_oom_handling))]
use core::clone::TrivialClone;
#[cfg(not(no_global_oom_handling))]
use core::cmp::Ordering::{self, Less};
Expand Down Expand Up @@ -660,6 +662,16 @@ impl [u8] {
pub fn to_ascii_lowercase(&self) -> Vec<u8> {
self.iter().map(|b| b.to_ascii_lowercase()).collect()
}

/// Converts a `Box<[u8]>` into a `Box<ByteStr>` without copying or allocating.
#[cfg(not(no_global_oom_handling))]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "byte_str", issue = "134915")]
#[inline]
pub fn into_boxed_byte_str(self: Box<[u8]>) -> Box<ByteStr> {
// SAFETY: `ByteStr` is a thin wrapper over `[u8]`
unsafe { Box::from_raw(Box::into_raw(self) as _) }
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
13 changes: 13 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub use self::extract_if::ExtractIf;
use crate::alloc::{Allocator, Global};
use crate::borrow::{Cow, ToOwned};
use crate::boxed::Box;
#[cfg(not(no_global_oom_handling))]
use crate::byte_str::ByteString;
use crate::collections::TryReserveError;
use crate::raw_vec::RawVec;

Expand Down Expand Up @@ -3663,6 +3665,17 @@ impl<A: Allocator> Vec<u8, A> {
}
}

impl Vec<u8> {
/// Converts a vector of bytes into a byte string.
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "byte_str", issue = "134915")]
#[inline]
#[rustc_const_unstable(feature = "byte_str", issue = "134915")]
pub const fn into_byte_string(self) -> ByteString {
ByteString(self)
}
}

impl<T, A: Allocator, const N: usize> Vec<[T; N], A> {
/// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use alloc::bstr::ByteString;
use alloc::byte_str::ByteStr;
use core::assert_matches;

#[test]
fn test_debug() {
let b1 = ByteString(
b"\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff".to_vec()
);
let b1 = ByteStr::new(
b"\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff"
).to_byte_string();
assert_eq!(
r#""\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff""#,
format!("{:?}", b1),
Expand All @@ -14,8 +14,8 @@ fn test_debug() {

#[test]
fn test_display() {
let b1 = ByteString(b"abc".to_vec());
let b2 = ByteString(b"\xf0\x28\x8c\xbc".to_vec());
let b1 = ByteStr::new(b"abc").to_byte_string();
let b2 = ByteStr::new(b"\xf0\x28\x8c\xbc").to_byte_string();

assert_eq!(&format!("{b1}"), "abc");
assert_eq!(&format!("{b2}"), "�(��");
Expand Down Expand Up @@ -72,8 +72,8 @@ fn test_display() {

#[test]
fn test_to_string() {
let b1 = ByteString(b"abc".to_vec());
let b2 = ByteString(b"\xf0\x28\x8c\xbc".to_vec());
let b1 = ByteStr::new(b"abc").to_byte_string();
let b2 = ByteStr::new(b"\xf0\x28\x8c\xbc").to_byte_string();

assert_eq!(Ok("abc".to_string()), b1.to_string());
assert_matches!(b2.to_string(), Err(core::str::Utf8Error { .. }));
Expand Down
6 changes: 3 additions & 3 deletions library/alloctests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#![feature(binary_heap_into_iter_sorted)]
#![feature(binary_heap_pop_if)]
#![feature(borrowed_buf_init)]
#![feature(bstr)]
#![feature(bstr_to_string)]
#![feature(buf_read_has_data_left)]
#![feature(byte_str)]
#![feature(byte_str_to_string)]
#![feature(can_vector)]
#![feature(casefold)]
#![feature(const_btree_len)]
Expand Down Expand Up @@ -73,8 +73,8 @@ mod arc;
mod autotraits;
mod borrow;
mod boxed;
mod bstr;
mod btree_set_hash;
mod byte_str;
mod c_str;
mod c_str2;
mod collections;
Expand Down
Loading
Loading