Skip to content

Commit 47f74b2

Browse files
committed
Add .as_bytes() -> &[u8] to ASCII strings.
1 parent e34883a commit 47f74b2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ impl OwnedAsciiCast<[u8]> for Vec<u8> {
262262
pub trait AsciiStr for Sized? {
263263
/// Convert to a string.
264264
fn as_str<'a>(&'a self) -> &'a str;
265+
266+
/// Convert to bytes.
267+
fn as_bytes<'a>(&'a self) -> &'a [u8];
265268
}
266269

267270
#[experimental = "may be replaced by generic conversion traits"]
@@ -270,6 +273,11 @@ impl AsciiStr for [Ascii] {
270273
fn as_str<'a>(&'a self) -> &'a str {
271274
unsafe { mem::transmute(self) }
272275
}
276+
277+
#[inline]
278+
fn as_bytes<'a>(&'a self) -> &'a [u8] {
279+
unsafe { mem::transmute(self) }
280+
}
273281
}
274282

275283
impl IntoString for Vec<Ascii> {
@@ -356,6 +364,12 @@ mod tests {
356364
assert_eq!(v.as_str(), "( ;");
357365
}
358366

367+
#[test]
368+
fn test_ascii_as_bytes() {
369+
let v = v2ascii!([40, 32, 59]);
370+
assert_eq!(v.as_bytes(), b"( ;");
371+
}
372+
359373
#[test]
360374
fn test_ascii_into_string() {
361375
assert_eq!(vec2ascii![40, 32, 59].into_string(), "( ;".to_string());

0 commit comments

Comments
 (0)