Skip to content

Instantly share code, notes, and snippets.

@olback
Last active August 13, 2020 22:54
Show Gist options
  • 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()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment