File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ mod test_readme {
1212
1313use delegate:: delegate;
1414use std:: fmt:: Display ;
15+ use std:: ops:: Deref ;
1516
1617#[ cfg( feature = "serde" ) ]
1718mod serde_support;
@@ -128,6 +129,14 @@ impl AsRef<String> for NonEmptyString {
128129 }
129130}
130131
132+ impl Deref for NonEmptyString {
133+ type Target = str ;
134+
135+ fn deref ( & self ) -> & Self :: Target {
136+ self . 0 . deref ( )
137+ }
138+ }
139+
131140impl < ' s > TryFrom < & ' s str > for NonEmptyString {
132141 type Error = & ' s str ;
133142
@@ -205,4 +214,17 @@ mod tests {
205214 println ! ( "{}" , & str ) ;
206215 assert_eq ! ( String :: from( "string" ) , str . to_string( ) )
207216 }
217+
218+
219+ fn print_str ( str : & str ) {
220+ println ! ( "{str}" )
221+ }
222+
223+ #[ test]
224+ fn deref_works ( ) {
225+ let str = "My String" ;
226+ let non_empty_string = NonEmptyString :: try_from ( str) . unwrap ( ) ;
227+ print_str ( & non_empty_string) ;
228+ assert_eq ! ( str , non_empty_string. deref( ) ) ;
229+ }
208230}
You can’t perform that action at this time.
0 commit comments