10
10
use crate :: prelude:: * ;
11
11
12
12
use core:: { fmt, iter} ;
13
+ use core:: convert:: TryFrom ;
13
14
14
15
use crate :: io;
15
16
use io:: Read as _;
@@ -46,8 +47,25 @@ impl CommandString {
46
47
///
47
48
/// Returns an error if and only if the string is
48
49
/// larger than 12 characters in length.
50
+ #[ deprecated( note = "Use `TryFrom::try_from` or `CommandString::try_from_static`" , since = "0.29.0" ) ]
49
51
pub fn try_from < S : Into < Cow < ' static , str > > > ( s : S ) -> Result < CommandString , CommandStringError > {
50
- let cow = s. into ( ) ;
52
+ Self :: try_from_static_cow ( s. into ( ) )
53
+ }
54
+
55
+ /// Convert `&'static str` to `CommandString`
56
+ ///
57
+ /// This is more efficient for string literals than non-static conversions because it avoids
58
+ /// allocation.
59
+ ///
60
+ /// # Errors
61
+ ///
62
+ /// Returns an error if and only if the string is
63
+ /// larger than 12 characters in length.
64
+ pub fn try_from_static ( s : & ' static str ) -> Result < CommandString , CommandStringError > {
65
+ Self :: try_from_static_cow ( s. into ( ) )
66
+ }
67
+
68
+ fn try_from_static_cow ( cow : Cow < ' static , str > ) -> Result < CommandString , CommandStringError > {
51
69
if cow. len ( ) > 12 {
52
70
Err ( CommandStringError { cow } )
53
71
} else {
@@ -56,6 +74,38 @@ impl CommandString {
56
74
}
57
75
}
58
76
77
+ impl TryFrom < String > for CommandString {
78
+ type Error = CommandStringError ;
79
+
80
+ fn try_from ( value : String ) -> Result < Self , Self :: Error > {
81
+ Self :: try_from_static_cow ( value. into ( ) )
82
+ }
83
+ }
84
+
85
+ impl TryFrom < Box < str > > for CommandString {
86
+ type Error = CommandStringError ;
87
+
88
+ fn try_from ( value : Box < str > ) -> Result < Self , Self :: Error > {
89
+ Self :: try_from_static_cow ( String :: from ( value) . into ( ) )
90
+ }
91
+ }
92
+
93
+ impl < ' a > TryFrom < & ' a str > for CommandString {
94
+ type Error = CommandStringError ;
95
+
96
+ fn try_from ( value : & ' a str ) -> Result < Self , Self :: Error > {
97
+ Self :: try_from_static_cow ( value. to_owned ( ) . into ( ) )
98
+ }
99
+ }
100
+
101
+ impl core:: str:: FromStr for CommandString {
102
+ type Err = CommandStringError ;
103
+
104
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
105
+ Self :: try_from_static_cow ( s. to_owned ( ) . into ( ) )
106
+ }
107
+ }
108
+
59
109
impl fmt:: Display for CommandString {
60
110
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
61
111
f. write_str ( self . 0 . as_ref ( ) )
@@ -264,7 +314,7 @@ impl NetworkMessage {
264
314
pub fn command ( & self ) -> CommandString {
265
315
match * self {
266
316
NetworkMessage :: Unknown { command : ref c, .. } => c. clone ( ) ,
267
- _ => CommandString :: try_from ( self . cmd ( ) ) . expect ( "cmd returns valid commands" )
317
+ _ => CommandString :: try_from_static ( self . cmd ( ) ) . expect ( "cmd returns valid commands" )
268
318
}
269
319
}
270
320
}
@@ -523,8 +573,8 @@ mod test {
523
573
#[ test]
524
574
fn commandstring_test ( ) {
525
575
// Test converting.
526
- assert_eq ! ( CommandString :: try_from ( "AndrewAndrew" ) . unwrap( ) . as_ref( ) , "AndrewAndrew" ) ;
527
- assert ! ( CommandString :: try_from ( "AndrewAndrewA" ) . is_err( ) ) ;
576
+ assert_eq ! ( CommandString :: try_from_static ( "AndrewAndrew" ) . unwrap( ) . as_ref( ) , "AndrewAndrew" ) ;
577
+ assert ! ( CommandString :: try_from_static ( "AndrewAndrewA" ) . is_err( ) ) ;
528
578
529
579
// Test serializing.
530
580
let cs = CommandString ( "Andrew" . into ( ) ) ;
@@ -534,7 +584,7 @@ mod test {
534
584
let cs: Result < CommandString , _ > = deserialize ( & [ 0x41u8 , 0x6e , 0x64 , 0x72 , 0x65 , 0x77 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
535
585
assert ! ( cs. is_ok( ) ) ;
536
586
assert_eq ! ( cs. as_ref( ) . unwrap( ) . to_string( ) , "Andrew" . to_owned( ) ) ;
537
- assert_eq ! ( cs. unwrap( ) , CommandString :: try_from ( "Andrew" ) . unwrap( ) ) ;
587
+ assert_eq ! ( cs. unwrap( ) , CommandString :: try_from_static ( "Andrew" ) . unwrap( ) ) ;
538
588
539
589
let short_cs: Result < CommandString , _ > = deserialize ( & [ 0x41u8 , 0x6e , 0x64 , 0x72 , 0x65 , 0x77 , 0 , 0 , 0 , 0 , 0 ] ) ;
540
590
assert ! ( short_cs. is_err( ) ) ;
0 commit comments