Last active
August 13, 2020 22:54
-
-
Save olback/b6764f5de1c57cc04ba82b4a43954daa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait ToKey { | |
fn to_key(&self) -> Vec<u8>; | |
} | |
impl ToKey for [u8; 32] { | |
fn to_key(&self) -> Vec<u8> { | |
(&self[..]).to_key() | |
} | |
} | |
impl ToKey for &[u8] { | |
fn to_key(&self) -> Vec<u8> { | |
self.to_vec() | |
} | |
} | |
impl ToKey for Vec<u8> { | |
fn to_key(&self) -> Vec<u8> { | |
self.clone() | |
} | |
} | |
impl ToKey for &str { | |
fn to_key(&self) -> Vec<u8> { | |
base64::decode(&self).unwrap() | |
} | |
} | |
impl ToKey for String { | |
fn to_key(&self) -> Vec<u8> { | |
(&**self).to_key() | |
} | |
} | |
impl ToKey for &String { | |
fn to_key(&self) -> Vec<u8> { | |
(*self).to_key() | |
} | |
} | |
struct Foo { | |
bar: Vec<u8> | |
} | |
impl Foo { | |
fn new(key: &dyn ToKey) -> Self { | |
Self { | |
bar: key.to_key() | |
} | |
} | |
} | |
Foo::new(&vec); | |
Foo::new(&slice); | |
Foo::new(&[100, 183, 53, 126, 165, 0, 93, 255, 148, 76, 14, 247, 102, 240, 37, 255, 142, 53, 138, 255, 138, 163, 117, 138, 70, 182, 240, 249, 244, 107, 162, 151]); | |
Foo::new(&"ZLc1fqUAXf+UTA73ZvAl/441iv+Ko3WKRrbw+fRropc="); | |
Foo::new(&"ZLc1fqUAXf+UTA73ZvAl/441iv+Ko3WKRrbw+fRropc=".to_string()); | |
Foo::new(&&"ZLc1fqUAXf+UTA73ZvAl/441iv+Ko3WKRrbw+fRropc=".to_string()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment