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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2017 Fabian Schuiki

//! A reference counted string that acts like a regular str slice, hiding the
//! fact that it is wrapped in `Rc`.
//!
//! # Example
//! ```
//! use rcstr::RcStr;
//! use std::collections::HashSet;
//!
//! let mut map: HashSet<RcStr> = HashSet::new();
//! map.insert(RcStr::new("foo"));
//!
//! assert!(map.contains("foo"));
//! assert!(map.contains(&RcStr::new("foo")));
//! assert!(!map.contains("bar"));
//! ```

use std::borrow::Borrow;
use std::cmp::Ordering;
use std::fmt;
use std::ops::Deref;
use std::rc::Rc;

#[derive(Clone, Hash, PartialEq, PartialOrd)]
pub struct RcStr(Rc<String>);

impl RcStr {
	/// Create a new ref-counted string which is a copy of `value`.
	///
	/// # Example
	/// ```
	/// use rcstr::RcStr;
	/// use std::borrow::Borrow;
	/// let a = RcStr::new("foo");
	/// let b: &str = &a;
	/// assert_eq!(&*a, "foo");
	/// assert_eq!(*a, *"foo");
	/// assert_eq!(b, "foo");
	/// ```
	pub fn new<S: Into<String>>(value: S) -> RcStr {
		RcStr(Rc::new(value.into()))
	}
}

impl Eq for RcStr {}

impl Ord for RcStr {
	fn cmp(&self, other: &RcStr) -> Ordering {
		self[..].cmp(&other[..])
	}
}

impl fmt::Debug for RcStr {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self[..].fmt(f)
	}
}

impl fmt::Display for RcStr {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self[..].fmt(f)
	}
}

impl Borrow<str> for RcStr {
	fn borrow(&self) -> &str {
		&self.0[..]
	}
}

impl Deref for RcStr {
	type Target = str;
	fn deref(&self) -> &str {
		&self.0[..]
	}
}

impl AsRef<str> for RcStr {
	fn as_ref(&self) -> &str {
		&self.0[..]
	}
}