Add InlineStringError to strings module, rename ShString repr variant
This commit is contained in:
1 parent
74e2bbf406
commit
6211e60319
4 files changed
+27
-29
No files matched your search
+8
-10
@@ -26,9 +26,9 @@ impl<const N: usize> FixedString<N> {
|
|||||||
pub unsafe fn from_raw_slice(bytes: &[u8]) -> Result<Self, Error> {
|
pub unsafe fn from_raw_slice(bytes: &[u8]) -> Result<Self, Error> {
|
||||||
match bytes.try_into() {
|
match bytes.try_into() {
|
||||||
Ok(bytes) => Ok(Self::from_raw_array(bytes)),
|
Ok(bytes) => Ok(Self::from_raw_array(bytes)),
|
||||||
Err(_) => Err(Error::BadLength {
|
Err(_) => Err(Error {
|
||||||
expected: N,
|
expected_len: N,
|
||||||
actual: bytes.len(),
|
actual_len: bytes.len(),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,23 +175,21 @@ impl<const N: usize> fmt::Display for FixedString<N> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub struct Error {
|
||||||
BadLength { expected: usize, actual: usize },
|
expected_len: usize,
|
||||||
|
actual_len: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
|
||||||
Error::BadLength { expected, actual } => {
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"expected {} bytes of string data, found {} bytes",
|
"expected {} bytes of string data, found {} bytes",
|
||||||
expected, actual
|
self.expected_len,
|
||||||
|
self.actual_len
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl error::Error for Error {}
|
impl error::Error for Error {}
|
||||||
|
|
||||||
|
|||||||
+10
-10
@@ -46,7 +46,7 @@ impl<const N: usize> InlineString<N> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new<S>(s: &S) -> Result<Self, InlineStringError>
|
pub fn new<S>(s: &S) -> Result<Self, Error>
|
||||||
where
|
where
|
||||||
S: AsRef<str> + ?Sized,
|
S: AsRef<str> + ?Sized,
|
||||||
{
|
{
|
||||||
@@ -59,7 +59,7 @@ impl<const N: usize> InlineString<N> {
|
|||||||
let len = u8::try_from(s.len())
|
let len = u8::try_from(s.len())
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|len| (len <= Self::MAX_LEN).then_some(len))
|
.and_then(|len| (len <= Self::MAX_LEN).then_some(len))
|
||||||
.ok_or(InlineStringError {
|
.ok_or(Error {
|
||||||
max_len: N,
|
max_len: N,
|
||||||
actual_len: s.len(),
|
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> {
|
impl<'a, const N: usize> TryFrom<&'a str> for InlineString<N> {
|
||||||
type Error = InlineStringError;
|
type Error = Error;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
|
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> {
|
impl<const N: usize> TryFrom<String> for InlineString<N> {
|
||||||
type Error = InlineStringError;
|
type Error = Error;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_from(s: String) -> Result<Self, Self::Error> {
|
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> {
|
impl<'a, const N: usize> TryFrom<Cow<'a, str>> for InlineString<N> {
|
||||||
type Error = InlineStringError;
|
type Error = Error;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_from(s: Cow<'a, str>) -> Result<Self, Self::Error> {
|
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> {
|
impl<const N: usize> str::FromStr for InlineString<N> {
|
||||||
type Err = InlineStringError;
|
type Err = Error;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
@@ -249,12 +249,12 @@ impl<const N: usize> fmt::Display for InlineString<N> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct InlineStringError {
|
pub struct Error {
|
||||||
max_len: usize,
|
max_len: usize,
|
||||||
actual_len: usize,
|
actual_len: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InlineStringError {
|
impl Error {
|
||||||
pub fn max_len(&self) -> usize {
|
pub fn max_len(&self) -> usize {
|
||||||
self.max_len
|
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 {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
@@ -275,4 +275,4 @@ impl fmt::Display for InlineStringError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for InlineStringError {}
|
impl error::Error for Error {}
|
||||||
+1
-1
@@ -4,5 +4,5 @@ pub mod inline;
|
|||||||
pub mod shstring;
|
pub mod shstring;
|
||||||
|
|
||||||
pub use fixed::{FixedString, Error as FixedStringError};
|
pub use fixed::{FixedString, Error as FixedStringError};
|
||||||
pub use inline::InlineString;
|
pub use inline::{InlineString, Error as InlineStringError};
|
||||||
pub use shstring::{ShString, ShString22};
|
pub use shstring::{ShString, ShString22};
|
||||||
@@ -49,7 +49,7 @@ impl<const N: usize> ShString<N> {
|
|||||||
{
|
{
|
||||||
match InlineString::new(&s) {
|
match InlineString::new(&s) {
|
||||||
Ok(stack_buf) => Self(Repr::Inline(stack_buf)),
|
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 {
|
pub fn as_str(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
Self(Repr::Inline(buf)) => buf,
|
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 {
|
pub fn as_str_mut(&mut self) -> &mut str {
|
||||||
match self {
|
match self {
|
||||||
Self(Repr::Inline(buf)) => buf,
|
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 {
|
pub fn into_string(self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Self(Repr::Inline(buf)) => buf.into_string(),
|
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 {
|
pub fn len(&self) -> usize {
|
||||||
match self {
|
match self {
|
||||||
Self(Repr::Inline(buf)) => buf.len(),
|
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 {
|
pub fn is_empty(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self(Repr::Inline(buf)) => buf.is_empty(),
|
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 {
|
pub fn heap_allocated(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Self(Repr::Inline(_)) => false,
|
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)]
|
#[derive(Clone)]
|
||||||
enum Repr<const N: usize> {
|
enum Repr<const N: usize> {
|
||||||
Inline(InlineString<N>),
|
Inline(InlineString<N>),
|
||||||
Heap(Box<str>),
|
Boxed(Box<str>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in new issue
Block a user