1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Helper trait to take ownership of strings.

use std::rc::Rc;
use std::ops::Deref;
use std::borrow::Cow;

/// A managed string that permits immutable borrowing.
#[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub enum Cons<'el> {
    /// A borrowed string.
    Borrowed(&'el str),
    /// An owned string (deprecate?)
    Owned(String),
    /// A refcounted string.
    Rc(Rc<String>),
}

impl<'a> AsRef<str> for Cons<'a> {
    fn as_ref(&self) -> &str {
        use self::Cons::*;

        match *self {
            Borrowed(value) => value,
            Owned(ref value) => value,
            Rc(ref value) => value.as_ref(),
        }
    }
}

impl<'a> Deref for Cons<'a> {
    type Target = str;

    fn deref(&self) -> &str {
        self.as_ref()
    }
}

impl<'el> From<String> for Cons<'el> {
    fn from(value: String) -> Self {
        Cons::Owned(value)
    }
}

impl<'el> From<&'el str> for Cons<'el> {
    fn from(value: &'el str) -> Self {
        Cons::Borrowed(value)
    }
}

impl<'el> From<Rc<String>> for Cons<'el> {
    fn from(value: Rc<String>) -> Self {
        Cons::Rc(value)
    }
}

impl<'el> From<Cow<'el, str>> for Cons<'el> {
    fn from(value: Cow<'el, str>) -> Self {
        use self::Cow::*;

        match value {
            Owned(string) => Cons::Owned(string),
            Borrowed(string) => Cons::Borrowed(string),
        }
    }
}