strid_examples/
minimal.rs

1//! An example of constructing a basic strongly-typed wrapper around
2//! a custom string-like type.
3
4use strid::braid;
5
6/// A wrapper around a custom string-like type that implements the
7/// minimal set of required traits for a braid type
8#[braid(serde)]
9pub struct MinimalUsernameBuf(MinimalString);
10
11/// An example of a minimal string implementaiton that can be wrapped inside
12/// an owned braid type.
13#[derive(
14    Clone,
15    Debug,
16    Hash,
17    PartialEq,
18    Eq,
19    PartialOrd,
20    Ord,
21    serde::Serialize,
22    serde::Deserialize,
23    facet::Facet,
24)]
25#[facet(transparent)]
26pub struct MinimalString(String);
27
28impl From<String> for MinimalString {
29    fn from(s: String) -> Self {
30        MinimalString(s)
31    }
32}
33
34impl From<&'_ str> for MinimalString {
35    fn from(s: &str) -> Self {
36        MinimalString(s.into())
37    }
38}
39
40impl From<Box<str>> for MinimalString {
41    fn from(s: Box<str>) -> Self {
42        MinimalString(s.into())
43    }
44}
45
46impl AsRef<str> for MinimalString {
47    fn as_ref(&self) -> &str {
48        self.0.as_ref()
49    }
50}
51
52impl From<MinimalString> for String {
53    fn from(s: MinimalString) -> Self {
54        s.0
55    }
56}