Skip to content

Instantly share code, notes, and snippets.

@olback
Last active August 13, 2020 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olback/b6764f5de1c57cc04ba82b4a43954daa to your computer and use it in GitHub Desktop.
Save olback/b6764f5de1c57cc04ba82b4a43954daa to your computer and use it in GitHub Desktop.
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