1+ use std:: borrow:: Borrow ;
2+ use std:: cmp:: Ordering ;
3+ use std:: fmt:: { Display , Formatter } ;
4+ use std:: ops:: Deref ;
5+ use strumbra:: SharedString ;
6+
7+ #[ repr( transparent) ]
8+ #[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
9+ #[ derive( Clone , Debug , PartialEq , PartialOrd , Ord , Hash ) ]
10+ pub struct ParamString ( SharedString ) ;
11+
12+ impl Deref for ParamString {
13+ type Target = str ;
14+
15+ #[ inline]
16+ fn deref ( & self ) -> & Self :: Target {
17+ self . 0 . as_ref ( )
18+ }
19+ }
20+
21+ impl From < & str > for ParamString {
22+ #[ inline]
23+ fn from ( s : & str ) -> Self {
24+ ParamString ( SharedString :: try_from ( s)
25+ . expect ( "ParamString with more than 4294967295 characters. Parameter too large." ) )
26+ }
27+ }
28+
29+ impl From < & String > for ParamString {
30+ #[ inline]
31+ fn from ( s : & String ) -> Self {
32+ ParamString ( SharedString :: try_from ( s)
33+ . expect ( "ParamString with more than 4294967295 characters. Parameter too large." ) )
34+ }
35+ }
36+
37+
38+ impl From < String > for ParamString {
39+ #[ inline]
40+ fn from ( s : String ) -> Self {
41+ ParamString ( SharedString :: try_from ( s)
42+ . expect ( "ParamString with more than 4294967295 characters. Parameter too large." ) )
43+ }
44+ }
45+
46+ impl Display for ParamString {
47+ #[ inline]
48+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
49+ self . 0 . fmt ( f)
50+ }
51+ }
52+
53+ impl AsRef < str > for ParamString {
54+ #[ inline]
55+ fn as_ref ( & self ) -> & str {
56+ self . 0 . as_ref ( )
57+ }
58+ }
59+
60+ impl Borrow < str > for ParamString {
61+ fn borrow ( & self ) -> & str {
62+ self . 0 . as_ref ( )
63+ }
64+ }
65+
66+ impl PartialEq < & str > for ParamString {
67+ fn eq ( & self , other : & & str ) -> bool {
68+ self . 0 . as_str ( ) == * other
69+ }
70+ }
71+
72+ impl PartialEq < str > for ParamString {
73+ fn eq ( & self , other : & str ) -> bool {
74+ self . 0 . as_str ( ) == other
75+ }
76+ }
77+
78+ impl PartialEq < String > for ParamString {
79+ fn eq ( & self , other : & String ) -> bool {
80+ self . 0 . as_str ( ) == other
81+ }
82+ }
83+
84+ impl PartialEq < ParamString > for & str {
85+ fn eq ( & self , other : & ParamString ) -> bool {
86+ * self == other. as_ref ( )
87+ }
88+ }
89+
90+ impl PartialEq < ParamString > for String {
91+ fn eq ( & self , other : & ParamString ) -> bool {
92+ * self == other. as_ref ( )
93+ }
94+ }
95+
96+ impl PartialEq < ParamString > for & String {
97+ fn eq ( & self , other : & ParamString ) -> bool {
98+ * self == other. as_ref ( )
99+ }
100+ }
101+
102+
103+ impl Eq for ParamString { }
104+
105+ impl PartialOrd < String > for ParamString {
106+ fn partial_cmp ( & self , other : & String ) -> Option < Ordering > {
107+ self . 0 . partial_cmp ( other)
108+ }
109+ }
110+
111+ impl PartialOrd < str > for ParamString {
112+ fn partial_cmp ( & self , other : & str ) -> Option < Ordering > {
113+ self . 0 . partial_cmp ( other)
114+ }
115+ }
116+
117+ impl PartialOrd < & str > for ParamString {
118+ fn partial_cmp ( & self , other : & & str ) -> Option < Ordering > {
119+ self . 0 . partial_cmp ( * other)
120+ }
121+ }
122+
123+
124+ impl ParamString {
125+ /// Pushes a new string onto the `ParamString`.
126+ ///
127+ /// This could incur up to two allocations.
128+ pub fn push_str ( & mut self , s : & str ) {
129+ let new= format ! ( "{self}{s}" ) ;
130+ self . 0 = SharedString :: try_from ( new)
131+ . expect ( "ParamString with more than 4294967295 characters." ) ;
132+ }
133+
134+ // Extracts a string slice containing the entire `ParamString`.
135+ pub fn as_str ( & self ) -> & str {
136+ self . 0 . as_str ( )
137+ }
138+ }
139+
140+ #[ cfg( test) ]
141+ mod test {
142+ use super :: * ;
143+ #[ test]
144+ pub fn test_push_str ( ) {
145+ let mut string = ParamString :: from ( "Hello" ) ;
146+ string. push_str ( " World" ) ;
147+ assert_eq ! ( "Hello World" , string) ;
148+ }
149+ }
0 commit comments