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
use std::borrow::Borrow;
use std::hash::Hash;
use std::rc::Rc;
use std::sync::Arc;

/// An interface for any `Clone`able wrapper around a `String`.
pub trait StringWrapper: Borrow<String> + Clone + Eq + From<String> + Hash {
    /// Unwrap to an owned `String`.
    fn into_owned(self) -> String;
    /// Borrow the contents as a slice.
    fn as_str(&self) -> &str;
}

impl StringWrapper for String {
    fn into_owned(self) -> String {
        self
    }

    fn as_str(&self) -> &str {
        self
    }
}

impl StringWrapper for Box<String> {
    #[cfg_attr(feature = "clippy", allow(boxed_local))]
    fn into_owned(self) -> String {
        *self
    }

    fn as_str(&self) -> &str {
        self
    }
}

impl StringWrapper for Rc<String> {
    fn into_owned(self) -> String {
        match Rc::try_unwrap(self) {
            Ok(s) => s,
            Err(rc) => (*rc).clone(),
        }
    }

    fn as_str(&self) -> &str {
        self
    }
}

impl StringWrapper for Arc<String> {
    fn into_owned(self) -> String {
        match Arc::try_unwrap(self) {
            Ok(s) => s,
            Err(arc) => (*arc).clone(),
        }
    }

    fn as_str(&self) -> &str {
        self
    }
}