[][src]Macro validated_slice::impl_slice_spec_methods

macro_rules! impl_slice_spec_methods {
    (
        field=$field:tt;
        methods=[$($method:ident),* $(,)?];
    ) => { ... };
    (@impl; ($field:tt); as_inner) => { ... };
    (@impl; ($field:tt); as_inner_mut) => { ... };
    (@impl; ($field:tt); from_inner_unchecked) => { ... };
    (@impl; ($field:tt); from_inner_unchecked_mut) => { ... };
}

Implements some methods of SliceSpec trait automatically.

This macro can be safely used in nostd environment.

Examples

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct AsciiError {
    valid_up_to: usize,
}
pub struct AsciiStr(str);

enum AsciiStrSpec {}

impl validated_slice::SliceSpec for AsciiStrSpec {
    type Custom = AsciiStr;
    type Inner = str;
    type Error = AsciiError;

    #[inline]
    fn validate(s: &Self::Inner) -> Result<(), Self::Error> {
        match s.as_bytes().iter().position(|b| !b.is_ascii()) {
            Some(pos) => Err(AsciiError { valid_up_to: pos }),
            None => Ok(()),
        }
    }

    validated_slice::impl_slice_spec_methods! {
        field=0;
        methods=[
            as_inner,
            as_inner_mut,
            from_inner_unchecked,
            from_inner_unchecked_mut,
        ];
    }
}

Field

For tuple struct, field is the index of the inner slice field. For usual struct, field is the identifier of the field.

Methods

List methods to implement automatically. validate is not supported and should be manually implemented by the user.