strings: refactor error types
This patch replaces the error type for `FixedString` and removes the re-exports of the various string error types in the `strings` module.
This commit is contained in:
1 parent
352c01f613
commit
96daa5ca00
2 files changed
+19
-30
No files matched your search
+17
-28
@@ -7,13 +7,25 @@ use core::{
|
|||||||
str,
|
str,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct LengthError;
|
||||||
|
|
||||||
|
impl fmt::Display for LengthError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "invalid string length for `FixedString`")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl std::error::Error for LengthError {}
|
||||||
|
|
||||||
pub struct FixedString<const N: usize> {
|
pub struct FixedString<const N: usize> {
|
||||||
buf: [u8; N],
|
buf: [u8; N],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> FixedString<N> {
|
impl<const N: usize> FixedString<N> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(s: &str) -> Result<Self, Error> {
|
pub fn new(s: &str) -> Result<Self, LengthError> {
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// A `&str` is always valid UTF-8.
|
// A `&str` is always valid UTF-8.
|
||||||
unsafe { Self::from_raw_slice(s.as_bytes()) }
|
unsafe { Self::from_raw_slice(s.as_bytes()) }
|
||||||
@@ -22,15 +34,12 @@ impl<const N: usize> FixedString<N> {
|
|||||||
/// # Safety
|
/// # Safety
|
||||||
/// The provided byte slice must be valid UTF-8.
|
/// The provided byte slice must be valid UTF-8.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn from_raw_slice(bytes: &[u8]) -> Result<Self, Error> {
|
pub unsafe fn from_raw_slice(bytes: &[u8]) -> Result<Self, LengthError> {
|
||||||
match bytes.try_into() {
|
match bytes.try_into() {
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// The caller is reponsible for ensuring that the provided bytes are valid UTF-8.
|
// The caller is reponsible for ensuring that the provided bytes are valid UTF-8.
|
||||||
Ok(bytes) => unsafe { Ok(Self::from_raw_array(bytes)) },
|
Ok(bytes) => unsafe { Ok(Self::from_raw_array(bytes)) },
|
||||||
Err(_) => Err(Error {
|
Err(_) => Err(LengthError),
|
||||||
expected_len: N,
|
|
||||||
actual_len: bytes.len(),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +125,7 @@ impl<const N: usize> borrow::BorrowMut<str> for FixedString<N> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> str::FromStr for FixedString<N> {
|
impl<const N: usize> str::FromStr for FixedString<N> {
|
||||||
type Err = Error;
|
type Err = LengthError;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
@@ -125,7 +134,7 @@ impl<const N: usize> str::FromStr for FixedString<N> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, const N: usize> TryFrom<&'a str> for FixedString<N> {
|
impl<'a, const N: usize> TryFrom<&'a str> for FixedString<N> {
|
||||||
type Error = Error;
|
type Error = LengthError;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
|
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
|
||||||
@@ -175,26 +184,6 @@ impl<const N: usize> fmt::Display for FixedString<N> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Error {
|
|
||||||
expected_len: usize,
|
|
||||||
actual_len: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"expected {} bytes of string data, found {} bytes",
|
|
||||||
self.expected_len,
|
|
||||||
self.actual_len
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
impl std::error::Error for Error {}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::FixedString;
|
use super::FixedString;
|
||||||
|
|||||||
+2
-2
@@ -3,7 +3,7 @@ pub mod capped;
|
|||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
pub mod inlining;
|
pub mod inlining;
|
||||||
|
|
||||||
pub use fixed::{FixedString, Error as FixedStringError};
|
pub use fixed::FixedString;
|
||||||
pub use capped::{CappedString, CapacityError as CappedStringError};
|
pub use capped::CappedString;
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
pub use inlining::{InliningString, InliningString23};
|
pub use inlining::{InliningString, InliningString23};
|
||||||
Reference in new issue
Block a user