[−][src]Macro validated_slice::impl_cmp_for_slice
Implements PartialEq and PartialOrd for the given custom slice type.
Usage
Examples
validated_slice::impl_cmp_for_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: AsciiStrSpec, custom: AsciiStr, inner: str, base: Inner, }; Cmp { PartialEq, PartialOrd }; // This is same as `#[derive(PartialEq, PartialOrd)]`. { ({Custom}), ({Custom}) }; { ({Custom}), (&{Custom}), rev }; // NOTE: `std::borrow::ToOwned for AsciiStr` is required by `Cow`. { ({Custom}), (Cow<{Custom}>), rev }; { ({Custom}), ({Inner}), rev }; { ({Custom}), (&{Inner}), 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:
// 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_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_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} and {Inner} will be replaced to the custom 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}, and its
variations), that type should implement AsRef<base_type>.
Supported types
{Custom}&{Custom}Cow<{Custom}>{Inner}&{Inner}Cow<{Inner}>- ... and arbitrary types
Note that, with base: Custom, {Inner} and its variants are not supported (because it does
not make sense).