[−][src]Trait validated_slice::OwnedSliceSpec
A trait to provide types and features for an owned custom slice type.
Safety
To avoid undefined behavior, users are responsible to let implementations satisfy all conditions below:
- Safety conditions for
Self::SliceSpec
is satisfied. Self::SliceCustom
is set to<Self::SliceSpec as SliceSpec>::Custom
.Self::SliceInner
is set to<Self::SliceSpec as SliceSpec>::Inner
.Self::SliceError
is set to<Self::SliceSpec as SliceSpec>::Error
.
If any of the conditions is not met, use of methods may cause undefined behavior.
Examples
/// ASCII string boxed slice. #[derive(Default, Debug, Clone, Eq, Ord, Hash)] pub struct AsciiString(String); enum AsciiStringSpec {} impl validated_slice::OwnedSliceSpec for AsciiStringSpec { type Custom = AsciiString; type Inner = String; // You can use dedicated error type for owned slice here, // as `std::str::Utf8Error` is used for borrowed slice validation and // `std::string::FromUtf8Error` is used for owned slice validation. type Error = AsciiError; type SliceSpec = AsciiStrSpec; type SliceCustom = AsciiStr; type SliceInner = str; type SliceError = AsciiError; #[inline] fn convert_validation_error(e: Self::SliceError, _: Self::Inner) -> Self::Error { e } #[inline] fn as_slice_inner(s: &Self::Custom) -> &Self::SliceInner { &s.0 } #[inline] fn as_slice_inner_mut(s: &mut Self::Custom) -> &mut Self::SliceInner { &mut s.0 } #[inline] fn inner_as_slice_inner(s: &Self::Inner) -> &Self::SliceInner { s } #[inline] unsafe fn from_inner_unchecked(s: Self::Inner) -> Self::Custom { AsciiString(s) } #[inline] fn into_inner(s: Self::Custom) -> Self::Inner { s.0 } }
Associated Types
type Custom
Custom owned slice type.
type Inner
Owned inner slice type of Self::Custom
.
type Error
Validation error type for owned inner type.
type SliceSpec: SliceSpec
Spec of the borrowed slice type.
type SliceCustom: ?Sized
Same type as <Self::SliceSpec as SliceSpec>::Custom
.
type SliceInner: ?Sized
Same type as <Self::SliceSpec as SliceSpec>::Inner
.
type SliceError
Same type as <Self::SliceSpec as SliceSpec>::Error
.
Required methods
fn convert_validation_error(e: Self::SliceError, v: Self::Inner) -> Self::Error
Converts a borrowed slice validation error into an owned slice validation error.
fn as_slice_inner(s: &Self::Custom) -> &Self::SliceInner
Returns the borrowed inner slice for the given reference to a custom owned slice.
fn as_slice_inner_mut(s: &mut Self::Custom) -> &mut Self::SliceInner
Returns the borrowed inner slice for the given mutable reference to a custom owned slice.
fn inner_as_slice_inner(s: &Self::Inner) -> &Self::SliceInner
Returns the borrowed inner slice for the given reference to owned inner slice.
unsafe fn from_inner_unchecked(s: Self::Inner) -> Self::Custom
Creates a reference to the custom slice type without any validation.
Safety
This is safe only when all of the conditions below are met:
Self::validate(s)
returnsOk(())
.- Safety condition for
Self::SliceSpec
is satisfied.
If any of the condition is not met, this function may cause undefined behavior.
fn into_inner(s: Self::Custom) -> Self::Inner
Returns the inner value with its ownership.