Add InlineStringError to strings module, rename ShString repr variant

main
Pantonshire 3 years ago
parent 74e2bbf406
commit 6211e60319

@ -26,9 +26,9 @@ impl<const N: usize> FixedString<N> {
pub unsafe fn from_raw_slice(bytes: &[u8]) -> Result<Self, Error> {
match bytes.try_into() {
Ok(bytes) => Ok(Self::from_raw_array(bytes)),
Err(_) => Err(Error::BadLength {
expected: N,
actual: bytes.len(),
Err(_) => Err(Error {
expected_len: N,
actual_len: bytes.len(),
}),
}
}
@ -175,23 +175,21 @@ impl<const N: usize> fmt::Display for FixedString<N> {
}
#[derive(Debug)]
pub enum Error {
BadLength { expected: usize, actual: usize },
pub struct Error {
expected_len: usize,
actual_len: usize,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::BadLength { expected, actual } => {
write!(
f,
"expected {} bytes of string data, found {} bytes",
expected, actual
self.expected_len,
self.actual_len
)
}
}
}
}
impl error::Error for Error {}

@ -46,7 +46,7 @@ impl<const N: usize> InlineString<N> {
}
#[inline]
pub fn new<S>(s: &S) -> Result<Self, InlineStringError>
pub fn new<S>(s: &S) -> Result<Self, Error>
where
S: AsRef<str> + ?Sized,
{
@ -59,7 +59,7 @@ impl<const N: usize> InlineString<N> {
let len = u8::try_from(s.len())
.ok()
.and_then(|len| (len <= Self::MAX_LEN).then_some(len))
.ok_or(InlineStringError {
.ok_or(Error {
max_len: N,
actual_len: s.len(),
})?;
@ -162,7 +162,7 @@ impl<const N: usize> borrow::BorrowMut<str> for InlineString<N> {
}
impl<'a, const N: usize> TryFrom<&'a str> for InlineString<N> {
type Error = InlineStringError;
type Error = Error;
#[inline]
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
@ -171,7 +171,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for InlineString<N> {
}
impl<const N: usize> TryFrom<String> for InlineString<N> {
type Error = InlineStringError;
type Error = Error;
#[inline]
fn try_from(s: String) -> Result<Self, Self::Error> {
@ -180,7 +180,7 @@ impl<const N: usize> TryFrom<String> for InlineString<N> {
}
impl<'a, const N: usize> TryFrom<Cow<'a, str>> for InlineString<N> {
type Error = InlineStringError;
type Error = Error;
#[inline]
fn try_from(s: Cow<'a, str>) -> Result<Self, Self::Error> {
@ -226,7 +226,7 @@ impl<const N: usize> Hash for InlineString<N> {
}
impl<const N: usize> str::FromStr for InlineString<N> {
type Err = InlineStringError;
type Err = Error;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -249,12 +249,12 @@ impl<const N: usize> fmt::Display for InlineString<N> {
}
#[derive(Debug)]
pub struct InlineStringError {
pub struct Error {
max_len: usize,
actual_len: usize,
}
impl InlineStringError {
impl Error {
pub fn max_len(&self) -> usize {
self.max_len
}
@ -264,7 +264,7 @@ impl InlineStringError {
}
}
impl fmt::Display for InlineStringError {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
@ -275,4 +275,4 @@ impl fmt::Display for InlineStringError {
}
}
impl error::Error for InlineStringError {}
impl error::Error for Error {}

@ -4,5 +4,5 @@ pub mod inline;
pub mod shstring;
pub use fixed::{FixedString, Error as FixedStringError};
pub use inline::InlineString;
pub use inline::{InlineString, Error as InlineStringError};
pub use shstring::{ShString, ShString22};

@ -49,7 +49,7 @@ impl<const N: usize> ShString<N> {
{
match InlineString::new(&s) {
Ok(stack_buf) => Self(Repr::Inline(stack_buf)),
Err(_) => Self(Repr::Heap(Box::<str>::from(s))),
Err(_) => Self(Repr::Boxed(Box::<str>::from(s))),
}
}
@ -59,7 +59,7 @@ impl<const N: usize> ShString<N> {
pub fn as_str(&self) -> &str {
match self {
Self(Repr::Inline(buf)) => buf,
Self(Repr::Heap(buf)) => buf,
Self(Repr::Boxed(buf)) => buf,
}
}
@ -69,7 +69,7 @@ impl<const N: usize> ShString<N> {
pub fn as_str_mut(&mut self) -> &mut str {
match self {
Self(Repr::Inline(buf)) => buf,
Self(Repr::Heap(buf)) => buf,
Self(Repr::Boxed(buf)) => buf,
}
}
@ -79,7 +79,7 @@ impl<const N: usize> ShString<N> {
pub fn into_string(self) -> String {
match self {
Self(Repr::Inline(buf)) => buf.into_string(),
Self(Repr::Heap(buf)) => buf.into_string(),
Self(Repr::Boxed(buf)) => buf.into_string(),
}
}
@ -95,7 +95,7 @@ impl<const N: usize> ShString<N> {
pub fn len(&self) -> usize {
match self {
Self(Repr::Inline(buf)) => buf.len(),
Self(Repr::Heap(buf)) => buf.len(),
Self(Repr::Boxed(buf)) => buf.len(),
}
}
@ -114,7 +114,7 @@ impl<const N: usize> ShString<N> {
pub fn is_empty(&self) -> bool {
match self {
Self(Repr::Inline(buf)) => buf.is_empty(),
Self(Repr::Heap(buf)) => buf.is_empty(),
Self(Repr::Boxed(buf)) => buf.is_empty(),
}
}
@ -133,7 +133,7 @@ impl<const N: usize> ShString<N> {
pub fn heap_allocated(&self) -> bool {
match self {
Self(Repr::Inline(_)) => false,
Self(Repr::Heap(_)) => true,
Self(Repr::Boxed(_)) => true,
}
}
}
@ -294,7 +294,7 @@ impl<'de, const N: usize> serde::Deserialize<'de> for ShString<N> {
#[derive(Clone)]
enum Repr<const N: usize> {
Inline(InlineString<N>),
Heap(Box<str>),
Boxed(Box<str>),
}
#[cfg(test)]

Loading…
Cancel
Save