From a84ea6922db91b996071f9ded32c39ff8589d1c0 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 01:17:12 +0300 Subject: [PATCH 01/10] fix UB W/o #[repr(transparent)] it's unsound to transmute between &T and &W(T) --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 9bc9109..068635d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,11 +39,13 @@ use std::ops::{Deref, DerefMut}; /// Wraps a byte slice and provides a `Debug` implementation /// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). +#[repr(transparent)] #[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ByteStr(pub [u8]); /// Wraps a vector of bytes and provides a `Debug` implementation /// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). +#[repr(transparent)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ByteString(pub Vec); From 77cb4d1d60edcd8f716e69d779e4e9bd91de5fc8 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 01:28:02 +0300 Subject: [PATCH 02/10] move to edition 2018 --- Cargo.toml | 1 + src/lib.rs | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0997357..4dd3ee6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "byte_string" version = "1.0.0" +edition = "2018" authors = ["Francis Gagné "] description = "Wrapper types for outputting byte strings (b\"Hello\") using the Debug ({:?}) format." documentation = "https://docs.rs/byte_string/" diff --git a/src/lib.rs b/src/lib.rs index 068635d..dc12b66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,7 +45,6 @@ pub struct ByteStr(pub [u8]); /// Wraps a vector of bytes and provides a `Debug` implementation /// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). -#[repr(transparent)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ByteString(pub Vec); @@ -167,11 +166,11 @@ impl<'a> IntoIterator for &'a mut ByteStr { impl Debug for ByteStr { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { - try!(write!(f, "b\"")); + write!(f, "b\"")?; for &byte in self { for ch in std::ascii::escape_default(byte) { - try!(write!(f, "{}", ch as char)); + write!(f, "{}", ch as char)?; } } From e488ab62261d1673784974b61581c36ccfd3f18a Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 01:59:49 +0300 Subject: [PATCH 03/10] add `no_std` support --- Cargo.toml | 4 + src/lib.rs | 400 ++++++++--------------------------------------------- 2 files changed, 61 insertions(+), 343 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4dd3ee6..1ca4474 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,7 @@ license = "MIT/Apache-2.0" exclude = [".travis.yml"] [dependencies] + +[features] +default = ["std"] +std = [] diff --git a/src/lib.rs b/src/lib.rs index dc12b66..15bdb0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,327 +28,32 @@ //! prefer exposing the underlying slice or vector instead. //! However, `ByteStr` and `ByteString` implement many traits, including derivable traits, //! which makes them suitable for use as a private member of a struct or enum. +//! +//! ## `no_std` support +//! +//! When built without default features (namely `std`) this crate supports `#![no_std]` +//! (though note that `ByteString` is supported only with `std` feature). #![warn(missing_docs)] +#![cfg_attr(all(not(feature = "std"), not(test)), no_std)] -use std::borrow::{Borrow, BorrowMut}; -use std::fmt::{Debug, Error, Formatter}; -use std::iter::FromIterator; -use std::mem; -use std::ops::{Deref, DerefMut}; - -/// Wraps a byte slice and provides a `Debug` implementation -/// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). -#[repr(transparent)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ByteStr(pub [u8]); - -/// Wraps a vector of bytes and provides a `Debug` implementation -/// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ByteString(pub Vec); - -impl ByteStr { - /// Converts an immutable byte slice to an immutable `ByteStr` reference. - pub fn new(s: &[u8]) -> &ByteStr { - unsafe { mem::transmute(s) } - } - - /// Converts a mutable byte slice to a mutable `ByteStr` reference. - pub fn new_mut(s: &mut [u8]) -> &mut ByteStr { - unsafe { mem::transmute(s) } - } -} - -impl<'a> From<&'a [u8]> for &'a ByteStr { - fn from(s: &[u8]) -> &ByteStr { - ByteStr::new(s) - } -} - -impl<'a> From<&'a mut [u8]> for &'a mut ByteStr { - fn from(s: &mut [u8]) -> &mut ByteStr { - ByteStr::new_mut(s) - } -} - -impl<'a> From<&'a ByteStr> for &'a [u8] { - fn from(s: &ByteStr) -> &[u8] { - &s.0 - } -} - -impl<'a> From<&'a mut ByteStr> for &'a mut [u8] { - fn from(s: &mut ByteStr) -> &mut [u8] { - &mut s.0 - } -} - -impl AsRef<[u8]> for ByteStr { - fn as_ref(&self) -> &[u8] { - &self.0 - } -} - -impl AsRef for [u8] { - fn as_ref(&self) -> &ByteStr { - ByteStr::new(self) - } -} - -impl AsMut<[u8]> for ByteStr { - fn as_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} +pub use self::str::ByteStr; -impl AsMut for [u8] { - fn as_mut(&mut self) -> &mut ByteStr { - ByteStr::new_mut(self) - } -} +#[cfg(feature = "std")] +pub use self::string::ByteString; -impl PartialEq<[u8]> for ByteStr { - fn eq(&self, other: &[u8]) -> bool { - &self.0 == other - } -} +mod str; -impl PartialEq for [u8] { - fn eq(&self, other: &ByteStr) -> bool { - self == &other.0 - } -} - -impl Deref for ByteStr { - type Target = [u8]; - - fn deref(&self) -> &[u8] { - &self.0 - } -} - -impl DerefMut for ByteStr { - fn deref_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} - -impl<'a> Default for &'a ByteStr { - fn default() -> &'a ByteStr { - ByteStr::new(&[]) - } -} - -impl<'a> Default for &'a mut ByteStr { - fn default() -> &'a mut ByteStr { - ByteStr::new_mut(&mut []) - } -} - -impl<'a> IntoIterator for &'a ByteStr { - type Item = &'a u8; - type IntoIter = std::slice::Iter<'a, u8>; - - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } -} - -impl<'a> IntoIterator for &'a mut ByteStr { - type Item = &'a mut u8; - type IntoIter = std::slice::IterMut<'a, u8>; - - fn into_iter(self) -> Self::IntoIter { - (&mut self.0).into_iter() - } -} - -impl Debug for ByteStr { - fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { - write!(f, "b\"")?; - - for &byte in self { - for ch in std::ascii::escape_default(byte) { - write!(f, "{}", ch as char)?; - } - } - - write!(f, "\"") - } -} - -impl ByteString { - /// Moves a vector of bytes to a new `ByteString`. - pub fn new(s: Vec) -> ByteString { - ByteString(s) - } -} - -impl From> for ByteString { - fn from(s: Vec) -> ByteString { - ByteString::new(s) - } -} - -impl From for Vec { - fn from(s: ByteString) -> Vec { - s.0 - } -} - -impl AsRef> for ByteString { - fn as_ref(&self) -> &Vec { - &self.0 - } -} - -impl AsRef<[u8]> for ByteString { - fn as_ref(&self) -> &[u8] { - &self.0 - } -} - -impl AsMut> for ByteString { - fn as_mut(&mut self) -> &mut Vec { - &mut self.0 - } -} - -impl AsMut<[u8]> for ByteString { - fn as_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} - -impl Borrow for ByteString { - fn borrow(&self) -> &ByteStr { - ByteStr::new(&self.0) - } -} - -impl Borrow> for ByteString { - fn borrow(&self) -> &Vec { - &self.0 - } -} - -impl Borrow<[u8]> for ByteString { - fn borrow(&self) -> &[u8] { - &self.0 - } -} - -impl BorrowMut for ByteString { - fn borrow_mut(&mut self) -> &mut ByteStr { - ByteStr::new_mut(&mut self.0) - } -} - -impl BorrowMut> for ByteString { - fn borrow_mut(&mut self) -> &mut Vec { - &mut self.0 - } -} - -impl BorrowMut<[u8]> for ByteString { - fn borrow_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} - -impl PartialEq> for ByteString { - fn eq(&self, other: &Vec) -> bool { - self.0 == *other - } -} - -impl PartialEq<[u8]> for ByteString { - fn eq(&self, other: &[u8]) -> bool { - self.0 == other - } -} - -impl PartialEq for Vec { - fn eq(&self, other: &ByteString) -> bool { - self == &other.0 - } -} - -impl PartialEq for [u8] { - fn eq(&self, other: &ByteString) -> bool { - self == &other.0[..] - } -} - -impl Deref for ByteString { - type Target = Vec; - - fn deref(&self) -> &Vec { - &self.0 - } -} - -impl DerefMut for ByteString { - fn deref_mut(&mut self) -> &mut Vec { - &mut self.0 - } -} - -impl Default for ByteString { - fn default() -> ByteString { - ByteString::new(vec![]) - } -} - -impl FromIterator for ByteString { - fn from_iter(iter: I) -> ByteString - where I: IntoIterator - { - ByteString::new(Vec::from_iter(iter)) - } -} - -impl<'a> IntoIterator for ByteString { - type Item = u8; - type IntoIter = std::vec::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } -} - -impl<'a> IntoIterator for &'a ByteString { - type Item = &'a u8; - type IntoIter = std::slice::Iter<'a, u8>; - - fn into_iter(self) -> Self::IntoIter { - (&self.0).into_iter() - } -} - -impl<'a> IntoIterator for &'a mut ByteString { - type Item = &'a mut u8; - type IntoIter = std::slice::IterMut<'a, u8>; - - fn into_iter(self) -> Self::IntoIter { - (&mut self.0).into_iter() - } -} - -impl Debug for ByteString { - fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { - // Delegate to ByteStr's implementation - Debug::fmt(Borrow::::borrow(self), f) - } -} +#[cfg(feature = "std")] +mod string; #[cfg(test)] mod tests { - use super::*; + use crate::*; const EMPTY: &'static str = "b\"\""; - const ALL_BYTES: &'static str = concat!("b\"", + const ALL_BYTES: &'static str = concat!( + "b\"", "\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f", "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f", " !\\\"#$%&\\\'()*+,-./", @@ -365,7 +70,8 @@ mod tests { "\\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf", "\\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef", "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff", - "\""); + "\"" + ); #[test] fn debug_bytestr_empty() { @@ -378,22 +84,25 @@ mod tests { #[test] fn debug_bytestr() { let bytes = [ - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, - 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, - 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, - 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, - 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C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0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, + 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, + 0xFC, 0xFD, 0xFE, 0xFF, ]; let bs = ByteStr::new(&bytes); let result = format!("{:?}", bs); @@ -401,6 +110,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn debug_bytestring_empty() { let bytes = vec![]; let bs = ByteString::new(bytes); @@ -409,24 +119,28 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn debug_bytestring() { let bytes = vec![ - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, - 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, - 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, - 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, - 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B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0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, + 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, + 0xFC, 0xFD, 0xFE, 0xFF, ]; let bs = ByteString::new(bytes); let result = format!("{:?}", bs); From ed2f3d476e853869b8b0cec0cffe35602f07a526 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 02:08:53 +0300 Subject: [PATCH 04/10] add `no_std` support --- src/str.rs | 141 +++++++++++++++++++++++++++++++++++++++ src/string.rs | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 320 insertions(+) create mode 100644 src/str.rs create mode 100644 src/string.rs diff --git a/src/str.rs b/src/str.rs new file mode 100644 index 0000000..2429ce8 --- /dev/null +++ b/src/str.rs @@ -0,0 +1,141 @@ +use core::{ + fmt::{self, Debug, Formatter}, + mem, + ops::{Deref, DerefMut}, +}; + +/// Wraps a byte slice and provides a `Debug` implementation +/// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). +#[repr(transparent)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct ByteStr(pub [u8]); + +impl ByteStr { + /// Converts an immutable byte slice to an immutable `ByteStr` reference. + pub fn new(s: &[u8]) -> &ByteStr { + unsafe { mem::transmute(s) } + } + + /// Converts a mutable byte slice to a mutable `ByteStr` reference. + pub fn new_mut(s: &mut [u8]) -> &mut ByteStr { + unsafe { mem::transmute(s) } + } +} + +impl<'a> From<&'a [u8]> for &'a ByteStr { + fn from(s: &[u8]) -> &ByteStr { + ByteStr::new(s) + } +} + +impl<'a> From<&'a mut [u8]> for &'a mut ByteStr { + fn from(s: &mut [u8]) -> &mut ByteStr { + ByteStr::new_mut(s) + } +} + +impl<'a> From<&'a ByteStr> for &'a [u8] { + fn from(s: &ByteStr) -> &[u8] { + &s.0 + } +} + +impl<'a> From<&'a mut ByteStr> for &'a mut [u8] { + fn from(s: &mut ByteStr) -> &mut [u8] { + &mut s.0 + } +} + +impl AsRef<[u8]> for ByteStr { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + +impl AsRef for [u8] { + fn as_ref(&self) -> &ByteStr { + ByteStr::new(self) + } +} + +impl AsMut<[u8]> for ByteStr { + fn as_mut(&mut self) -> &mut [u8] { + &mut self.0 + } +} + +impl AsMut for [u8] { + fn as_mut(&mut self) -> &mut ByteStr { + ByteStr::new_mut(self) + } +} + +impl PartialEq<[u8]> for ByteStr { + fn eq(&self, other: &[u8]) -> bool { + &self.0 == other + } +} + +impl PartialEq for [u8] { + fn eq(&self, other: &ByteStr) -> bool { + self == &other.0 + } +} + +impl Deref for ByteStr { + type Target = [u8]; + + fn deref(&self) -> &[u8] { + &self.0 + } +} + +impl DerefMut for ByteStr { + fn deref_mut(&mut self) -> &mut [u8] { + &mut self.0 + } +} + +impl<'a> Default for &'a ByteStr { + fn default() -> &'a ByteStr { + ByteStr::new(&[]) + } +} + +impl<'a> Default for &'a mut ByteStr { + fn default() -> &'a mut ByteStr { + ByteStr::new_mut(&mut []) + } +} + +impl<'a> IntoIterator for &'a ByteStr { + type Item = &'a u8; + type IntoIter = core::slice::Iter<'a, u8>; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl<'a> IntoIterator for &'a mut ByteStr { + type Item = &'a mut u8; + type IntoIter = core::slice::IterMut<'a, u8>; + + fn into_iter(self) -> Self::IntoIter { + (&mut self.0).into_iter() + } +} + +impl Debug for ByteStr { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { + write!(f, "b\"")?; + + for &byte in self { + for ch in core::ascii::escape_default(byte) { + write!(f, "{}", ch as char)?; + } + } + + write!(f, "\"") + } +} diff --git a/src/string.rs b/src/string.rs new file mode 100644 index 0000000..f2da2d5 --- /dev/null +++ b/src/string.rs @@ -0,0 +1,179 @@ +use core::{ + borrow::{Borrow, BorrowMut}, + fmt::{self, Debug, Formatter}, + iter::FromIterator, + ops::{Deref, DerefMut}, +}; + +use crate::ByteStr; + +/// Wraps a vector of bytes and provides a `Debug` implementation +/// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct ByteString(pub Vec); + +impl ByteString { + /// Moves a vector of bytes to a new `ByteString`. + pub fn new(s: Vec) -> ByteString { + ByteString(s) + } +} + +impl From> for ByteString { + fn from(s: Vec) -> ByteString { + ByteString::new(s) + } +} + +impl From for Vec { + fn from(s: ByteString) -> Vec { + s.0 + } +} + +impl AsRef> for ByteString { + fn as_ref(&self) -> &Vec { + &self.0 + } +} + +impl AsRef<[u8]> for ByteString { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + +impl AsMut> for ByteString { + fn as_mut(&mut self) -> &mut Vec { + &mut self.0 + } +} + +impl AsMut<[u8]> for ByteString { + fn as_mut(&mut self) -> &mut [u8] { + &mut self.0 + } +} + +impl Borrow for ByteString { + fn borrow(&self) -> &ByteStr { + ByteStr::new(&self.0) + } +} + +impl Borrow> for ByteString { + fn borrow(&self) -> &Vec { + &self.0 + } +} + +impl Borrow<[u8]> for ByteString { + fn borrow(&self) -> &[u8] { + &self.0 + } +} + +impl BorrowMut for ByteString { + fn borrow_mut(&mut self) -> &mut ByteStr { + ByteStr::new_mut(&mut self.0) + } +} + +impl BorrowMut> for ByteString { + fn borrow_mut(&mut self) -> &mut Vec { + &mut self.0 + } +} + +impl BorrowMut<[u8]> for ByteString { + fn borrow_mut(&mut self) -> &mut [u8] { + &mut self.0 + } +} + +impl PartialEq> for ByteString { + fn eq(&self, other: &Vec) -> bool { + self.0 == *other + } +} + +impl PartialEq<[u8]> for ByteString { + fn eq(&self, other: &[u8]) -> bool { + self.0 == other + } +} + +impl PartialEq for Vec { + fn eq(&self, other: &ByteString) -> bool { + self == &other.0 + } +} + +impl PartialEq for [u8] { + fn eq(&self, other: &ByteString) -> bool { + self == &other.0[..] + } +} + +impl Deref for ByteString { + type Target = Vec; + + fn deref(&self) -> &Vec { + &self.0 + } +} + +impl DerefMut for ByteString { + fn deref_mut(&mut self) -> &mut Vec { + &mut self.0 + } +} + +impl Default for ByteString { + fn default() -> ByteString { + ByteString::new(vec![]) + } +} + +impl FromIterator for ByteString { + fn from_iter(iter: I) -> ByteString + where + I: IntoIterator, + { + ByteString::new(Vec::from_iter(iter)) + } +} + +impl<'a> IntoIterator for ByteString { + type Item = u8; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl<'a> IntoIterator for &'a ByteString { + type Item = &'a u8; + type IntoIter = core::slice::Iter<'a, u8>; + + fn into_iter(self) -> Self::IntoIter { + (&self.0).into_iter() + } +} + +impl<'a> IntoIterator for &'a mut ByteString { + type Item = &'a mut u8; + type IntoIter = core::slice::IterMut<'a, u8>; + + fn into_iter(self) -> Self::IntoIter { + (&mut self.0).into_iter() + } +} + +impl Debug for ByteString { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { + // Delegate to ByteStr's implementation + Debug::fmt(Borrow::::borrow(self), f) + } +} From b92cfa0a6614734c6d89edbdb5799b6dd9792d45 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 02:10:48 +0300 Subject: [PATCH 05/10] remove unsafe code use ref-cast crate instead --- Cargo.toml | 1 + src/str.rs | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1ca4474..a66a7ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ license = "MIT/Apache-2.0" exclude = [".travis.yml"] [dependencies] +ref-cast = "1.0.3" [features] default = ["std"] diff --git a/src/str.rs b/src/str.rs index 2429ce8..22d1f76 100644 --- a/src/str.rs +++ b/src/str.rs @@ -1,24 +1,25 @@ use core::{ fmt::{self, Debug, Formatter}, - mem, ops::{Deref, DerefMut}, }; +use ref_cast::RefCast; + /// Wraps a byte slice and provides a `Debug` implementation /// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). #[repr(transparent)] -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, ref_cast::RefCast)] pub struct ByteStr(pub [u8]); impl ByteStr { /// Converts an immutable byte slice to an immutable `ByteStr` reference. pub fn new(s: &[u8]) -> &ByteStr { - unsafe { mem::transmute(s) } + Self::ref_cast(s) } /// Converts a mutable byte slice to a mutable `ByteStr` reference. pub fn new_mut(s: &mut [u8]) -> &mut ByteStr { - unsafe { mem::transmute(s) } + Self::ref_cast_mut(s) } } From e10187609d700a1fc8fd51f647a3b8ff5443df0f Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 02:16:50 +0300 Subject: [PATCH 06/10] impl Display for {ByteStr,ByteString} --- src/lib.rs | 2 +- src/str.rs | 14 +++++++++++++- src/string.rs | 8 ++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 15bdb0f..639f311 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ //! //! ## `no_std` support //! -//! When built without default features (namely `std`) this crate supports `#![no_std]` +//! When built without default features (namely `std`) this crate supports `#![no_std]` //! (though note that `ByteString` is supported only with `std` feature). #![warn(missing_docs)] diff --git a/src/str.rs b/src/str.rs index 22d1f76..8dfb4a3 100644 --- a/src/str.rs +++ b/src/str.rs @@ -1,5 +1,5 @@ use core::{ - fmt::{self, Debug, Formatter}, + fmt::{self, Debug, Display, Formatter}, ops::{Deref, DerefMut}, }; @@ -140,3 +140,15 @@ impl Debug for ByteStr { write!(f, "\"") } } + +impl Display for ByteStr { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { + for &byte in self { + for ch in core::ascii::escape_default(byte) { + write!(f, "{}", ch as char)?; + } + } + + Ok(()) + } +} diff --git a/src/string.rs b/src/string.rs index f2da2d5..b2dd1ae 100644 --- a/src/string.rs +++ b/src/string.rs @@ -1,5 +1,6 @@ use core::{ borrow::{Borrow, BorrowMut}, + fmt::Display, fmt::{self, Debug, Formatter}, iter::FromIterator, ops::{Deref, DerefMut}, @@ -177,3 +178,10 @@ impl Debug for ByteString { Debug::fmt(Borrow::::borrow(self), f) } } + +impl Display for ByteString { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { + // Delegate to ByteStr's implementation + Display::fmt(Borrow::::borrow(self), f) + } +} From 3a93462c8cdef73a7a5c525eaa6b4ebbfc01230f Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 03:10:17 +0300 Subject: [PATCH 07/10] adjust docs --- README.md | 16 ++++++++++------ src/lib.rs | 28 +++++++++++++++------------- src/str.rs | 4 ++-- src/string.rs | 4 ++-- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 1604d0e..3e868f6 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,15 @@ # byte_string [![Build Status](https://travis-ci.org/FraGag/byte_string.svg?branch=master)](https://travis-ci.org/FraGag/byte_string) -The `byte_string` crate provides two types: `ByteStr` and `ByteString`. -Both types provide a `Debug` implementation -that outputs the slice using the Rust byte string syntax. -`ByteStr` wraps a byte slice (`[u8]`). +The `byte_string` crate provides two types: `ByteStr` and `ByteString`. +Both types provide a `Debug` implementation +that outputs the slice using the Rust byte string syntax +and a `Display` implementation with similar output, but without b"". +`ByteStr` wraps a byte slice (`[u8]`). `ByteString` wraps a vector of bytes (`Vec`). For example: ```rust -extern crate byte_string; - use byte_string::ByteStr; fn main() { @@ -31,6 +30,11 @@ prefer exposing the underlying slice or vector instead. However, `ByteStr` and `ByteString` implement many traits, including derivable traits, which makes them suitable for use as a private member of a struct or enum. +## `no_std` support + +When built without default features (namely `std`) this crate supports `#![no_std]` +(though note that `ByteString` is supported only with `std` feature). + ## License byte_string is licensed diff --git a/src/lib.rs b/src/lib.rs index 639f311..8935ae8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,34 +1,36 @@ -//! The `byte_string` crate provides two types: `ByteStr` and `ByteString`. -//! Both types provide a `Debug` implementation -//! that outputs the slice using the Rust byte string syntax. -//! `ByteStr` wraps a byte slice (`[u8]`). -//! `ByteString` wraps a vector of bytes (`Vec`). +//! The `byte_string` crate provides two types: [`ByteStr`] and [`ByteString`]. +//! Both types provide a [`Debug`] implementation +//! that outputs the slice using the Rust byte string syntax and a +//! [`Display`] implementation with similar output, but without `b""`. +//! [`ByteStr`] wraps a byte slice (`[u8]`). +//! [`ByteString`] wraps a vector of bytes (`Vec`). //! //! For example: //! //! ``` -//! extern crate byte_string; -//! //! use byte_string::ByteStr; //! //! fn main() { //! let s = b"Hello, world!"; //! let bs = ByteStr::new(s); //! assert_eq!(format!("{:?}", bs), "b\"Hello, world!\""); +//! assert_eq!(format!("{}", bs), "Hello, world!"); //! } //! ``` //! -//! `ByteStr` is an unsized type, as `[u8]` is. -//! `ByteStr::new()` returns a `&ByteStr` -//! and `ByteStr::new_mut()` returns a `&mut ByteStr`. +//! [`ByteStr`] is an unsized type, as `[u8]` is. +//! [`ByteStr::new()`] returns a `&ByteStr` +//! and [`ByteStr::new_mut()`] returns a `&mut ByteStr`. //! -//! `ByteStr` and `ByteString` are meant to be used as an implementation detail. -//! You should generally avoid exposing a `ByteStr` or a `ByteString` +//! [`ByteStr`] and [`ByteString`] are meant to be used as an implementation detail. +//! You should generally avoid exposing a [`ByteStr`] or a [`ByteString`] //! as part of a struct or enum; //! prefer exposing the underlying slice or vector instead. -//! However, `ByteStr` and `ByteString` implement many traits, including derivable traits, +//! However, [`ByteStr`] and [`ByteString`] implement many traits, including derivable traits, //! which makes them suitable for use as a private member of a struct or enum. //! +//! [`Display`]: core::fmt::Display +//! //! ## `no_std` support //! //! When built without default features (namely `std`) this crate supports `#![no_std]` diff --git a/src/str.rs b/src/str.rs index 8dfb4a3..1ccd150 100644 --- a/src/str.rs +++ b/src/str.rs @@ -5,8 +5,8 @@ use core::{ use ref_cast::RefCast; -/// Wraps a byte slice and provides a `Debug` implementation -/// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). +/// Wraps a byte slice and provides a [`Debug`] (and a [`Display`]) implementation +/// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"` (display will output `abc`)). #[repr(transparent)] #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, ref_cast::RefCast)] pub struct ByteStr(pub [u8]); diff --git a/src/string.rs b/src/string.rs index b2dd1ae..4fa8f7f 100644 --- a/src/string.rs +++ b/src/string.rs @@ -8,8 +8,8 @@ use core::{ use crate::ByteStr; -/// Wraps a vector of bytes and provides a `Debug` implementation -/// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"`). +/// Wraps a vector of bytes and provides a [`Debug`] (and a [`Display`]) implementation +/// that outputs the slice using the Rust byte string syntax (e.g. `b"abc"` (display will output `abc`)). #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ByteString(pub Vec); From 1f58fc39260a32632372140c69f7eaba016906d7 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 03:14:05 +0300 Subject: [PATCH 08/10] clippy --- src/lib.rs | 10 ++++------ src/str.rs | 4 ++-- src/string.rs | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8935ae8..97185f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,12 +10,10 @@ //! ``` //! use byte_string::ByteStr; //! -//! fn main() { -//! let s = b"Hello, world!"; -//! let bs = ByteStr::new(s); -//! assert_eq!(format!("{:?}", bs), "b\"Hello, world!\""); -//! assert_eq!(format!("{}", bs), "Hello, world!"); -//! } +//! let s = b"Hello, world!"; +//! let bs = ByteStr::new(s); +//! assert_eq!(format!("{:?}", bs), "b\"Hello, world!\""); +//! assert_eq!(format!("{}", bs), "Hello, world!"); //! ``` //! //! [`ByteStr`] is an unsized type, as `[u8]` is. diff --git a/src/str.rs b/src/str.rs index 1ccd150..f54f810 100644 --- a/src/str.rs +++ b/src/str.rs @@ -114,7 +114,7 @@ impl<'a> IntoIterator for &'a ByteStr { type IntoIter = core::slice::Iter<'a, u8>; fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() + self.0.iter() } } @@ -123,7 +123,7 @@ impl<'a> IntoIterator for &'a mut ByteStr { type IntoIter = core::slice::IterMut<'a, u8>; fn into_iter(self) -> Self::IntoIter { - (&mut self.0).into_iter() + self.0.iter_mut() } } diff --git a/src/string.rs b/src/string.rs index 4fa8f7f..947caec 100644 --- a/src/string.rs +++ b/src/string.rs @@ -159,7 +159,7 @@ impl<'a> IntoIterator for &'a ByteString { type IntoIter = core::slice::Iter<'a, u8>; fn into_iter(self) -> Self::IntoIter { - (&self.0).into_iter() + self.0.iter() } } @@ -168,7 +168,7 @@ impl<'a> IntoIterator for &'a mut ByteString { type IntoIter = core::slice::IterMut<'a, u8>; fn into_iter(self) -> Self::IntoIter { - (&mut self.0).into_iter() + self.0.iter_mut() } } From 6919f7065d41635673b4e5c0f8f917af993959c1 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 16 Nov 2020 03:18:11 +0300 Subject: [PATCH 09/10] bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a66a7ac..6e8a3fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "byte_string" -version = "1.0.0" +version = "1.1.0" edition = "2018" authors = ["Francis Gagné "] description = "Wrapper types for outputting byte strings (b\"Hello\") using the Debug ({:?}) format." From f03518f3c7ba6b8bf95a2cb2556188f101af9c9d Mon Sep 17 00:00:00 2001 From: Waffle Date: Sun, 22 Nov 2020 23:38:49 +0300 Subject: [PATCH 10/10] add tests for display impls --- src/lib.rs | 131 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 50 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 97185f6..69f5202 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,8 +51,29 @@ mod string; mod tests { use crate::*; - const EMPTY: &'static str = "b\"\""; - const ALL_BYTES: &'static str = concat!( + const ALL_BYTES: [u8; 256] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, + 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, + 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, + 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, + 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, + 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, + 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, + 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, + 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, + 0xFF, + ]; + + const EMPTY_DEBUG: &'static str = "b\"\""; + const ALL_BYTES_DEBUG: &'static str = concat!( "b\"", "\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f", "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f", @@ -78,35 +99,14 @@ mod tests { let bytes = []; let bs = ByteStr::new(&bytes); let result = format!("{:?}", bs); - assert_eq!(result, EMPTY); + assert_eq!(result, EMPTY_DEBUG); } #[test] fn debug_bytestr() { - let bytes = [ - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, - 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x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CF, 0xD0, 0xD1, - 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, - 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, - 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, - 0xFC, 0xFD, 0xFE, 0xFF, - ]; - let bs = ByteStr::new(&bytes); + let bs = ByteStr::new(&ALL_BYTES); let result = format!("{:?}", bs); - assert_eq!(result, ALL_BYTES); + assert_eq!(result, ALL_BYTES_DEBUG); } #[test] @@ -115,35 +115,66 @@ mod tests { let bytes = vec![]; let bs = ByteString::new(bytes); let result = format!("{:?}", bs); - assert_eq!(result, EMPTY); + assert_eq!(result, EMPTY_DEBUG); } #[test] #[cfg(feature = "std")] fn debug_bytestring() { - let bytes = vec![ - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, - 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, - 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, - 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, - 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, - 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, - 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, - 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, - 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, - 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, - 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, - 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, - 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, - 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, - 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, - 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, - 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, - 0xFC, 0xFD, 0xFE, 0xFF, - ]; - let bs = ByteString::new(bytes); + let bs = ByteString::new(ALL_BYTES.to_vec()); let result = format!("{:?}", bs); - assert_eq!(result, ALL_BYTES); + assert_eq!(result, ALL_BYTES_DEBUG); + } + + const EMPTY_DISPLAY: &'static str = ""; + const ALL_BYTES_DISPLAY: &'static str = concat!( + "\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f", + "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f", + " !\\\"#$%&\\\'()*+,-./", + "0123456789:;<=>?", + "@ABCDEFGHIJKLMNO", + "PQRSTUVWXYZ[\\\\]^_", + "`abcdefghijklmno", + "pqrstuvwxyz{|}~\\x7f", + "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f", + "\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f", + "\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf", + "\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf", + "\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf", + "\\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf", + "\\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef", + "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff", + ); + + #[test] + fn display_bytestr_empty() { + let bytes = []; + let bs = ByteStr::new(&bytes); + let result = format!("{}", bs); + assert_eq!(result, EMPTY_DISPLAY); + } + + #[test] + fn display_bytestr() { + let bs = ByteStr::new(&ALL_BYTES); + let result = format!("{}", bs); + assert_eq!(result, ALL_BYTES_DISPLAY); + } + + #[test] + #[cfg(feature = "std")] + fn display_bytestring_empty() { + let bytes = vec![]; + let bs = ByteString::new(bytes); + let result = format!("{}", bs); + assert_eq!(result, EMPTY_DISPLAY); + } + + #[test] + #[cfg(feature = "std")] + fn display_bytestring() { + let bs = ByteString::new(ALL_BYTES.to_vec()); + let result = format!("{}", bs); + assert_eq!(result, ALL_BYTES_DISPLAY); } }