[][src]Trait validated_slice::OwnedSliceSpec

pub trait OwnedSliceSpec {
    type Custom;
    type Inner;
    type Error;
    type SliceSpec: SliceSpec;
    type SliceCustom: ?Sized;
    type SliceInner: ?Sized;
    type SliceError;
    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; }

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

This example is not tested
/// ASCII string boxed slice.
#[derive(Default, Debug, Clone, Eq, Ord, Hash)]
pub struct AsciiString(String);

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

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.

Loading content...

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

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

Loading content...

Implementors

Loading content...