[][src]Macro validated_slice::impl_cmp_for_owned_slice

macro_rules! impl_cmp_for_owned_slice {
    (
        Spec {
            spec: $spec:ty,
            custom: $custom:ty,
            inner: $inner:ty,
            slice_custom: $slice_custom:ty,
            slice_inner: $slice_inner:ty,
            base: $base:ident,
        };
        Cmp { $($cmp_targets:ident),* };
        $($rest:tt)*
    ) => { ... };
    (
        Std {
            core: $core:ident,
            alloc: $alloc:ident,
        };
        Spec {
            spec: $spec:ty,
            custom: $custom:ty,
            inner: $inner:ty,
            slice_custom: $slice_custom:ty,
            slice_inner: $slice_inner:ty,
            base: $base:ident,
        };
        Cmp { $($cmp_targets:ident),* };
        $($rest:tt)*
    ) => { ... };
    (
        @full;
        Std {
            core: $core:ident,
            alloc: $alloc:ident,
        };
        Spec {
            spec: $spec:ty,
            custom: $custom:ty,
            inner: $inner:ty,
            slice_custom: $slice_custom:ty,
            slice_inner: $slice_inner:ty,
            base: $base:ident,
        };
        Cmp { PartialEq, PartialOrd };
        $({ ($($lhs:tt)*), ($($rhs:tt)*) $(, $($opt:ident),*)? });* $(;)?
    ) => { ... };
    (
        @full;
        Std {
            core: $core:ident,
            alloc: $alloc:ident,
        };
        Spec {
            spec: $spec:ty,
            custom: $custom:ty,
            inner: $inner:ty,
            slice_custom: $slice_custom:ty,
            slice_inner: $slice_inner:ty,
            base: $base:ident,
        };
        Cmp { PartialEq };
        $({ ($($lhs:tt)*), ($($rhs:tt)*) $(, $($opt:ident),*)? });* $(;)?
    ) => { ... };
    (
        @full;
        Std {
            core: $core:ident,
            alloc: $alloc:ident,
        };
        Spec {
            spec: $spec:ty,
            custom: $custom:ty,
            inner: $inner:ty,
            slice_custom: $slice_custom:ty,
            slice_inner: $slice_inner:ty,
            base: $base:ident,
        };
        Cmp { PartialOrd };
        $({ ($($lhs:tt)*), ($($rhs:tt)*) $(, $($opt:ident),*)? });* $(;)?
    ) => { ... };
    (
        @impl[PartialEq]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty, $base:ident);
        { ($($lhs:tt)*), ($($rhs:tt)*) };
    ) => { ... };
    (
        @impl[PartialEq]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty, $base:ident);
        { ($($lhs:tt)*), ($($rhs:tt)*), rev };
    ) => { ... };
    (
        @impl[PartialOrd]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty, $base:ident);
        { ($($lhs:tt)*), ($($rhs:tt)*) };
    ) => { ... };
    (
        @impl[PartialOrd]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty, $base:ident);
        { ($($lhs:tt)*), ($($rhs:tt)*), rev };
    ) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { {Custom} }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { &{Custom} }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { {SliceCustom} }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { &{SliceCustom} }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { Cow<{SliceCustom}> }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { {Inner} }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { &{Inner} }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { {SliceInner} }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { &{SliceInner} }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { Cow<{SliceInner}> }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { Cow<$ty:ty> }) => { ... };
    (@type; ({$core:ident, $alloc:ident}, $custom:ty, $inner:ty, $slice_custom:ty, $slice_inner:ty); { $ty:ty }) => { ... };
    (@cmp_fn[PartialEq]; ($slice_custom:ty, $slice_inner:ty, Inner)) => { ... };
    (@cmp_fn[PartialEq]; ($slice_custom:ty, $slice_inner:ty, Custom)) => { ... };
    (@cmp_fn[PartialOrd]; ($slice_custom:ty, $slice_inner:ty, Inner)) => { ... };
    (@cmp_fn[PartialOrd]; ($slice_custom:ty, $slice_inner:ty, Custom)) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { {Custom} }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { &{Custom} }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { Cow<{Custom}> }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { {SliceCustom} }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { &{SliceCustom} }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { Cow<{SliceCustom}> }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { {Inner} }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { &{Inner} }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { Cow<{Inner}> }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { {SliceInner} }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { &{SliceInner} }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { Cow<{SliceInner}> }; $expr:expr) => { ... };
    (@expr[Inner]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { $ty:ty }; $expr:expr) => { ... };
    (@expr[Custom]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { {Custom} }; $expr:expr) => { ... };
    (@expr[Custom]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { &{Custom} }; $expr:expr) => { ... };
    (@expr[Custom]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { Cow<{Custom}> }; $expr:expr) => { ... };
    (@expr[Custom]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { {SliceCustom} }; $expr:expr) => { ... };
    (@expr[Custom]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { &{SliceCustom} }; $expr:expr) => { ... };
    (@expr[Custom]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { Cow<{SliceCustom}> }; $expr:expr) => { ... };
    (@expr[Custom]; ({$core:ident, $alloc:ident}, $spec:ty, $custom:ty, $inner:ty); { $ty:ty }; $expr:expr) => { ... };
    ($($rest:tt)*) => { ... };
}

Implements PartialEq and PartialOrd for the given custom owned slice type.

Usage

Examples

This example is not tested
validated_slice::impl_cmp_for_owned_slice! {
    // `Std` is omissible.
    Std {
        // Module identifier of `core` crate.
        // Default is `std`.
        core: core,
        // Module identifier of `alloc` crate.
        // Default is `std`.
        alloc: alloc,
    };
    Spec {
        spec: AsciiStringSpec,
        custom: AsciiString,
        inner: String,
        slice_custom: AsciiStr,
        slice_inner: str,
        base: Inner,
    };
    Cmp { PartialEq, PartialOrd };
    // This is same as `#[derive(PartialEq, PartialOrd)]`.
    { ({Custom}), ({Custom}) };
    { ({Custom}), ({SliceCustom}), rev };
    { ({Custom}), (&{SliceCustom}), rev };
    // NOTE: `std::borrow::Borrow for AsciiString` is required by `Cow`.
    { ({Custom}), (Cow<{SliceCustom}>), rev };
    /* ... and more pairs! */
}

Core and alloc

For no_std use, the macro uses custom core and alloc crate if given. You can support both nostd and non-nostd environment as below:

This example is not tested
// Use `std` when available.
#[cfg(feature = "std")]
use alloc as std;
// Use external `alloc` crate when nostd.
#[cfg(not(feature = "std"))]
use alloc;

validated_slice::impl_cmp_for_owned_slice! {
    Std {
        core: core,
        alloc: alloc,
    }
    Spec { /* ... */ };
    Cmp { /* ... */ };
    /* ... */
}

When you don't need alloc crate on nostd build, value of alloc field is not used. Simply specify alloc: alloc, or something.

Comparison base

The syntax of Spec part is very similar to impl_std_traits_for_owned_slice! macro.

As base field, specify Custom or Inner to decide which comparison should be used internally. If you don't define custom comparison, use base: Inner.

Traits to implement

Comparison traits to implement is specified by Cmp { .. }; format. Supproted formats are: Cmp { PartialEq }, Cmp { PartialOrd }, and Cmp { PartialEq, PartialOrd };.

Operand type pairs

Comparisons are implemented between two types, so you should provide list of pairs to implement comparison.

Supported syntaxes are: { (lhs_ty), (rhs_ty) }; and { (lhs_ty), (rhs_ty), rev };.

Parentheses around types are not omittable.

With , rev, the macro implements not only PartialXx<rhs_ty> for lhs_ty, but also PartialXx<lhs_ty> for rhs_ty.

Type names

{Custom}, {Inner}, {SliceCustom}, and {SliceInner} will be replaced to the custom slice type, its inner type, custom borrowed slice type, and its inner type.

&ty and Cow<ty> are also supported.

Note that in case you specify arbitrary types (other than {Custom}, {Inner}, {SliceCustom}, {SliceInner}, and its variations), that type should implement AsRef<base_type>.

Supported types

  • {Custom}
  • &{Custom}
  • {SliceCustom}
  • &{SliceCustom}
  • Cow<{SliceCustom}>
  • {Inner}
  • &{Inner}
  • {SliceInner}
  • &{SliceInner}
  • Cow<{SliceInner}>
  • ... and arbitrary types

Note that, with base: Custom, {Inner}, {SliceInner} and its variants are not supported (because it does not make sense).