Skip to content

Commit f726735

Browse files
sunshowersThomas Bahn
authored and
Thomas Bahn
committed
implement AsRef<[u8]> for AsciiString
`String` implements `AsRef<[u8]>` as well.
1 parent 7503c68 commit f726735

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/ascii_string.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,13 @@ impl AsRef<AsciiStr> for AsciiString {
445445
}
446446
}
447447

448+
impl AsRef<[u8]> for AsciiString {
449+
#[inline]
450+
fn as_ref(&self) -> &[u8] {
451+
self.as_bytes()
452+
}
453+
}
454+
448455
impl AsMut<AsciiStr> for AsciiString {
449456
#[inline]
450457
fn as_mut(&mut self) -> &mut AsciiStr {

tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ fn into_ascii() {
3333
let arr = [AsciiChar::ParenOpen, AsciiChar::Space, AsciiChar::Semicolon];
3434
let v = AsciiString::from(arr.to_vec());
3535
assert_eq!(b"( ;".to_vec().into_ascii_string(), Ok(v.clone()));
36-
assert_eq!("( ;".to_string().into_ascii_string(), Ok(v));
36+
assert_eq!("( ;".to_string().into_ascii_string(), Ok(v.clone()));
37+
assert_eq!(b"( ;", AsRef::<[u8]>::as_ref(&v));
3738

3839
let err = "zoä华".to_string().into_ascii_string().unwrap_err();
3940
assert_eq!(Err(err.ascii_error()), "zoä华".as_ascii_str());

0 commit comments

Comments
 (0)