Skip to content

Instantly share code, notes, and snippets.

@olback
Last active August 13, 2020 22:54

Revisions

  1. olback revised this gist Aug 13, 2020. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions rocket-key.rs
    Original file line number Diff line number Diff line change
    @@ -51,3 +51,10 @@ impl Foo {
    }

    }

    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());
  2. olback created this gist Aug 13, 2020.
    53 changes: 53 additions & 0 deletions rocket-key.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    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()
    }
    }

    }