Implement From<String> and From<&'static str> for CommandString

This commit is contained in:
Steven Roose 2019-12-03 22:20:45 +00:00
parent 36838b7918
commit 50a37f415e
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
1 changed files with 13 additions and 1 deletions

View File

@ -43,6 +43,18 @@ impl fmt::Display for CommandString {
}
}
impl From<&'static str> for CommandString {
fn from(f: &'static str) -> Self {
CommandString(f.into())
}
}
impl From<String> for CommandString {
fn from(f: String) -> Self {
CommandString(f.into())
}
}
impl Encodable for CommandString {
#[inline]
fn consensus_encode<S: io::Write>(
@ -320,7 +332,7 @@ mod test {
let cs: Result<CommandString, _> = deserialize(&[0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0, 0]);
assert!(cs.is_ok());
assert_eq!(cs.as_ref().unwrap().to_string(), "Andrew".to_owned());
assert_eq!(cs.unwrap(), CommandString("Andrew".into()));
assert_eq!(cs.unwrap(), "Andrew".into());
let short_cs: Result<CommandString, _> = deserialize(&[0x41u8, 0x6e, 0x64, 0x72, 0x65, 0x77, 0, 0, 0, 0, 0]);
assert!(short_cs.is_err());