Trait OwnedSliceSpec

Source
pub trait OwnedSliceSpec {
    type Custom;
    type Inner;
    type Error;
    type SliceSpec: SliceSpec;
    type SliceCustom: ?Sized;
    type SliceInner: ?Sized;
    type SliceError;

    // Required methods
    fn convert_validation_error(
        e: Self::SliceError,
        v: Self::Inner,
    ) -> Self::Error;
    fn as_slice_inner(s: &Self::Custom) -> &Self::SliceInner;
    fn as_slice_inner_mut(s: &mut Self::Custom) -> &mut Self::SliceInner;
    fn inner_as_slice_inner(s: &Self::Inner) -> &Self::SliceInner;
    unsafe fn from_inner_unchecked(s: Self::Inner) -> Self::Custom;
    fn into_inner(s: Self::Custom) -> Self::Inner;
}
Expand description

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
    }
}

Required Associated Types§

Source

type Custom

Custom owned slice type.

Source

type Inner

Owned inner slice type of Self::Custom.

Source

type Error

Validation error type for owned inner type.

Source

type SliceSpec: SliceSpec

Spec of the borrowed slice type.

Source

type SliceCustom: ?Sized

Same type as <Self::SliceSpec as SliceSpec>::Custom.

Source

type SliceInner: ?Sized

Same type as <Self::SliceSpec as SliceSpec>::Inner.

Source

type SliceError

Same type as <Self::SliceSpec as SliceSpec>::Error.

Required Methods§

Source

fn convert_validation_error(e: Self::SliceError, v: Self::Inner) -> Self::Error

Converts a borrowed slice validation error into an owned slice validation error.

Source

fn as_slice_inner(s: &Self::Custom) -> &Self::SliceInner

Returns the borrowed inner slice for the given reference to a custom owned slice.

Source

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.

Source

fn inner_as_slice_inner(s: &Self::Inner) -> &Self::SliceInner

Returns the borrowed inner slice for the given reference to owned inner slice.

Source

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) returns Ok(()).
  • Safety condition for Self::SliceSpec is satisfied.

If any of the condition is not met, this function may cause undefined behavior.

Source

fn into_inner(s: Self::Custom) -> Self::Inner

Returns the inner value with its ownership.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§