#[derive(StrNewType)]
{
// Attributes available to this derive:
#[newtype]
}
Expand description
Derives methods and traits for an str new-type, along with an owned
companion type.
This derive macro must be used on a type of the form:
ⓘ
#[derive(StrNewType)]
#[newtype(...)]
pub struct Type(str);The target type must implement two const compatible validation methods:
validate_bytes(&[u8]) -> bool; andvalidate_str(&str) -> bool.
The macro will then derive various methods, types and trait implementations
depending on the sub-attributes specified in the newtype attribute.
§Derived items
Here is the full list of the derived items, depending on the sub-attributes
passed to the newtype attribute.
- If the
infalliblesub-attribute is not set:- Error type
Invalid{Type}<T = String>(pub T);withDebugimplementationDisplayimplementationErrorimplementation
Type::new<T: ?Sized + AsRef<[u8]>>(input: &T) -> Result<&Self, Invalid{Type}<&T>>constructor;const Type::from_bytes(input: &[u8]) -> Result<&Self, Invalid{Type}<&[u8]>>constructor;const Type::from_str(input: &str) -> Result<&Str, Invalid{Type}<&str>>constructor;&Type: TryFrom<&str>
- Error type
- If the
infalliblesub-attribute is set:Type::new<T: ?Sized + AsRef<[u8]>>(input: &T) -> &Selfconstructor;const Type::from_bytes(input: &[u8]) -> &Selfconstructor;const Type::from_str(input: &str) -> &Selfconstructor;&Type: From<&str>
&Type: TryFrom<&[u8]>Type: AsRef<Self>Type: AsRef<str>Type: AsRef<[u8]>Type: DisplayType: DebugType: Borrow<str>&str: From<&Type>&[u8]: From<&Type>- If the
noderefsub-attribute is not set:Type: Deref<str>
- If the
eq(Other)attribute is set:Type: PartialEq<Other>Other: PartialEq<Type>
- If the
ord(Other)attribute is set:Type: PartialOrd<Other>Other: PartialOrd<Type>
- If the
serdeattribute is set:Type: ::serde::Serialize&Type: ::serde::Deserialize<'_>
- If the
owned(OwnedType, ...)sub-attribute is set (where...denotes the owned-type sub-attributes):- If the
infalliblesub-attribute is not set:struct OwnedType(String)OwnedType::new<T: str_newtype::Buffer>(input: T) -> Result<Self, Invalid{Type}<T>>OwnedType::from_bytes(input: Vec<u8>) -> Result<Self, Invalid{Type}<Vec<u8>>>OwnedType::from_string(input: String) -> Result<Self, Invalid{Type}>unsafe OwnedType::new_unchecked(input: impl Into<Vec<u8>>) -> SelfOwnedType: TryFrom<String>
- If the
infalliblesub-attribute is set:struct OwnedType(pub String)OwnedType::new(input: impl Into<String>) -> SelfOwnedType::from_string(input: String) -> SelfOwnedType::from_bytes(input: Vec<u8>) -> Result<Self, ::std::string::FromUtf8Error>OwnedType: From<String>
OwnedType: DisplayOwnedType: DebugOwnedType: CloneOwnedType: FromStrType: ToOwned<Owned => OwnedType>OwnedType: Deref<Target = Type>OwnedType: TryFrom<Vec<u8>>OwnedType::as_{type}(&self) -> &Typewhere{type}is the camel case version ofType.OwnedType::as_str(&self) -> &strOwnedType::as_bytes(&self) -> &[u8]OwnedType::into_string(self) -> StringOwnedType::into_bytes(self) -> Vec<u8>OwnedType: Borrow<Type>OwnedType: AsRef<Type>OwnedType: AsRef<str>OwnedType: AsRef<[u8]>String: From<OwnedType>Vec<u8>: From<OwnedType>- If the
eq(Other)attribute is set:OwnedType: PartialEq<Other>Other: PartialEq<OwnedType>
- If the
ord(Other)attribute is set:OwnedType: PartialOrd<Other>Other: PartialOrd<OwnedType>
- If the
serdeattribute is set:OwnedType: ::serde::SerializeOwnedType: ::serde::Deserialize<'_>
- If the
derive(Default)owned-type sub-attribute is set:OwnedType: Default(requiresType: Default)
- If the
derive(PartialEq)owned-type sub-attribute is set:OwnedType: PartialEq(requiresType: PartialEq)
- If the
derive(Eq)owned-type sub-attribute is set:OwnedType: Eq(requiresType: Eq)
- If the
derive(PartialOrd)owned-type sub-attribute is set:OwnedType: PartialOrd(requiresType: PartialOrd)
- If the
derive(Ord)owned-type sub-attribute is set:OwnedType: Ord(requiresType: Ord)
- If the
derive(Hash)owned-type sub-attribute is set:OwnedType: Hash(requiresType: Hash)
- If the
§The newtype attribute
Generated items can be configured using the newtype attribute.
This attribute takes sub-attribute between parenthesis. For example:
ⓘ
#[derive(StrNewType)]
#[newtype(eq(str, [u8]), ord(str), noderef, owned(Foo, derive(Default, Hash)))]
pub struct Type(str);Here is the list of sub-attributes:
noderef: Prevent theType: Deref<Target = str>implementation.eq: ImplementType: PartialEq<Other>(andOwnedType: PartialEq<Other>if applicable) whereOthermust appear in a parenthesized comma-separated list after the sub-attribute (eq(A, B, C)).ord: ImplementType: PartialOrd<Other>(andOwnedType: PartialOrd<Other>if applicable) whereOthermust appear in a parenthesized comma-separated list after the sub-attribute (ord(A, B, C)).serde: ImplementType: Serialize + Deserialize(andOwnedType: Serialize + Deserializeif applicable)owned(OwnedType): Derive an owned variant ofTypecalledOwnedType. This sub-attribute can take additional owned-type sub-attributes after the identifier:derive: Specifies the list of trait to derive onOwnedType. Must be given as a parenthesized comma-separated list (e.g.derive(Default, Hash)). Possible traits are:DefaultPartialEqEqPartialOrdOrdHash