conch_runtime_pshaw/env/
string_wrapper.rs

1use std::borrow::Borrow;
2use std::hash::Hash;
3use std::rc::Rc;
4use std::sync::Arc;
5
6/// An interface for any `Clone`able wrapper around a `String`.
7pub trait StringWrapper: Borrow<String> + Clone + Eq + From<String> + Hash {
8    /// Unwrap to an owned `String`.
9    fn into_owned(self) -> String;
10    /// Borrow the contents as a slice.
11    fn as_str(&self) -> &str;
12}
13
14impl StringWrapper for String {
15    fn into_owned(self) -> String {
16        self
17    }
18
19    fn as_str(&self) -> &str {
20        self
21    }
22}
23
24impl StringWrapper for Box<String> {
25    #[allow(clippy::boxed_local)]
26    fn into_owned(self) -> String {
27        *self
28    }
29
30    fn as_str(&self) -> &str {
31        self
32    }
33}
34
35impl StringWrapper for Rc<String> {
36    fn into_owned(self) -> String {
37        match Rc::try_unwrap(self) {
38            Ok(s) => s,
39            Err(rc) => (*rc).clone(),
40        }
41    }
42
43    fn as_str(&self) -> &str {
44        self
45    }
46}
47
48impl StringWrapper for Arc<String> {
49    fn into_owned(self) -> String {
50        match Arc::try_unwrap(self) {
51            Ok(s) => s,
52            Err(arc) => (*arc).clone(),
53        }
54    }
55
56    fn as_str(&self) -> &str {
57        self
58    }
59}