Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f6d0bb5

Browse files
committedJan 4, 2025
Implement ByteStr and ByteString types
Approved ACP: rust-lang/libs-team#502 Tracking issue: #134915 These types represent human-readable strings that are conventionally, but not always, UTF-8. The `Debug` impl prints non-UTF-8 bytes using escape sequences, and the `Display` impl uses the Unicode replacement character. This is a minimal implementation of these types and associated trait impls. It does not add any helper methods to other types such as `[u8]` or `Vec<u8>`. I've omitted a few implementations of `AsRef`, `AsMut`, `Borrow`, and `From`, when those would be the second implementation for a type (counting the `T` impl), to avoid potential inference failures. These impls are important, but we can attempt to add them later in standalone commits, and run them through crater. In addition to the `bstr` feature, I've added a `bstr_internals` feature for APIs provided by `core` for use by `alloc` but not currently intended for stabilization. This API and its implementation are based *heavily* on the `bstr` crate by Andrew Gallant (@BurntSushi).
1 parent 7349f6b commit f6d0bb5

File tree

9 files changed

+1332
-2
lines changed

9 files changed

+1332
-2
lines changed
 

‎library/alloc/src/bstr.rs

Lines changed: 690 additions & 0 deletions
Large diffs are not rendered by default.

‎library/alloc/src/collections/btree/set/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ fn test_recovery() {
533533

534534
impl PartialOrd for Foo {
535535
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
536-
self.0.partial_cmp(&other.0)
536+
PartialOrd::partial_cmp(&self.0, &other.0)
537537
}
538538
}
539539

‎library/alloc/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
#![feature(async_fn_traits)]
104104
#![feature(async_iterator)]
105105
#![feature(box_uninit_write)]
106+
#![feature(bstr)]
107+
#![feature(bstr_internals)]
106108
#![feature(clone_to_uninit)]
107109
#![feature(coerce_unsized)]
108110
#![feature(const_eval_select)]
@@ -226,6 +228,8 @@ mod boxed {
226228
pub use std::boxed::Box;
227229
}
228230
pub mod borrow;
231+
#[unstable(feature = "bstr", issue = "134915")]
232+
pub mod bstr;
229233
pub mod collections;
230234
#[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
231235
pub mod ffi;

‎library/core/src/bstr.rs

Lines changed: 574 additions & 0 deletions
Large diffs are not rendered by default.

‎library/core/src/ffi/c_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ impl CStr {
679679
impl PartialOrd for CStr {
680680
#[inline]
681681
fn partial_cmp(&self, other: &CStr) -> Option<Ordering> {
682-
self.to_bytes().partial_cmp(&other.to_bytes())
682+
PartialOrd::partial_cmp(self.to_bytes(), other.to_bytes())
683683
}
684684
}
685685
#[stable(feature = "rust1", since = "1.0.0")]

‎library/core/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
#![feature(array_ptr_get)]
112112
#![feature(asm_experimental_arch)]
113113
#![feature(bigint_helper_methods)]
114+
#![feature(bstr)]
115+
#![feature(bstr_internals)]
114116
#![feature(const_carrying_mul_add)]
115117
#![feature(const_eval_select)]
116118
#![feature(core_intrinsics)]
@@ -336,6 +338,8 @@ pub mod ascii;
336338
pub mod asserting;
337339
#[unstable(feature = "async_iterator", issue = "79024")]
338340
pub mod async_iter;
341+
#[unstable(feature = "bstr", issue = "134915")]
342+
pub mod bstr;
339343
pub mod cell;
340344
pub mod char;
341345
pub mod ffi;

‎library/core/tests/bstr.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#[test]
2+
fn test_debug() {
3+
assert_eq!(
4+
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""#,
5+
format!("{:?}", BStr::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")),
6+
);
7+
}
8+
9+
#[test]
10+
fn test_display() {
11+
let b1 = ByteStr::new("abc");
12+
let b2 = ByteStr::new(b"\xf0\x28\x8c\xbc");
13+
14+
assert_eq!(&format!("{b1}"), "abc");
15+
assert_eq!(&format!("{b2}"), "�(��");
16+
17+
assert_eq!(&format!("{b1:<7}!"), "abc !");
18+
assert_eq!(&format!("{b1:>7}!"), " abc!");
19+
assert_eq!(&format!("{b1:^7}!"), " abc !");
20+
assert_eq!(&format!("{b1:^6}!"), " abc !");
21+
assert_eq!(&format!("{b1:-<7}!"), "abc----!");
22+
assert_eq!(&format!("{b1:->7}!"), "----abc!");
23+
assert_eq!(&format!("{b1:-^7}!"), "--abc--!");
24+
assert_eq!(&format!("{b1:-^6}!"), "-abc--!");
25+
26+
assert_eq!(&format!("{b2:<7}!"), "�(�� !");
27+
assert_eq!(&format!("{b2:>7}!"), " �(��!");
28+
assert_eq!(&format!("{b2:^7}!"), " �(�� !");
29+
assert_eq!(&format!("{b2:^6}!"), " �(�� !");
30+
assert_eq!(&format!("{b2:-<7}!"), "�(��---!");
31+
assert_eq!(&format!("{b2:->7}!"), "---�(��!");
32+
assert_eq!(&format!("{b2:-^7}!"), "-�(��--!");
33+
assert_eq!(&format!("{b2:-^6}!"), "-�(��-!");
34+
35+
assert_eq!(&format!("{b1:<2}!"), "abc!");
36+
assert_eq!(&format!("{b1:>2}!"), "abc!");
37+
assert_eq!(&format!("{b1:^2}!"), "abc!");
38+
assert_eq!(&format!("{b1:-<2}!"), "abc!");
39+
assert_eq!(&format!("{b1:->2}!"), "abc!");
40+
assert_eq!(&format!("{b1:-^2}!"), "abc!");
41+
42+
assert_eq!(&format!("{b2:<3}!"), "�(��!");
43+
assert_eq!(&format!("{b2:>3}!"), "�(��!");
44+
assert_eq!(&format!("{b2:^3}!"), "�(��!");
45+
assert_eq!(&format!("{b2:^2}!"), "�(��!");
46+
assert_eq!(&format!("{b2:-<3}!"), "�(��!");
47+
assert_eq!(&format!("{b2:->3}!"), "�(��!");
48+
assert_eq!(&format!("{b2:-^3}!"), "�(��!");
49+
assert_eq!(&format!("{b2:-^2}!"), "�(��!");
50+
}

‎library/std/src/bstr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! The `ByteStr` and `ByteString` types and trait implementations.
2+
3+
#[unstable(feature = "bstr", issue = "134915")]
4+
pub use alloc::bstr::{ByteStr, ByteString};

‎library/std/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@
320320
// Library features (core):
321321
// tidy-alphabetical-start
322322
#![feature(array_chunks)]
323+
#![feature(bstr)]
324+
#![feature(bstr_internals)]
323325
#![feature(c_str_module)]
324326
#![feature(char_internals)]
325327
#![feature(clone_to_uninit)]
@@ -580,6 +582,8 @@ pub mod f64;
580582
pub mod thread;
581583
pub mod ascii;
582584
pub mod backtrace;
585+
#[unstable(feature = "bstr", issue = "134915")]
586+
pub mod bstr;
583587
pub mod collections;
584588
pub mod env;
585589
pub mod error;

0 commit comments

Comments
 (0)
Please sign in to comment.