pub struct Key<T: Sized>(T);
impl<T> From<T> for Key<T> {
fn from(input: T) -> Self {
Key::<T> { 0: input }
}
}
impl Into<Key<String>> for Key<&str> {
fn into(self) -> Key<String> {
Key::<String> {
0: self.0.to_string(),
}
}
}
impl Into<Vec<u8>> for Key<u32> {
fn into(self) -> Vec<u8> {
self.0.to_be_bytes().to_vec()
}
}
impl Into<Vec<u8>> for Key<u64> {
fn into(self) -> Vec<u8> {
self.0.to_be_bytes().to_vec()
}
}
impl Into<Vec<u8>> for Key<String> {
fn into(self) -> Vec<u8> {
self.0.as_bytes().to_vec()
}
}
impl Into<Vec<u8>> for Key<&str> {
fn into(self) -> Vec<u8> {
self.0.as_bytes().to_vec()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Record;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Thing {
id: u32,
}
impl Record for Thing {
type Key = Key<u32>;
fn key(&self) -> Self::Key {
Key::from(self.id)
}
}
#[derive(Serialize, Deserialize)]
struct OtherThing {
id: std::string::String,
}
impl Record for OtherThing {
type Key = std::string::String;
fn key(&self) -> Self::Key {
self.id.clone()
}
}
#[derive(Serialize, Deserialize)]
struct AnotherThing {
id: u32,
}
impl Record for AnotherThing {
type Key = Key<u32>;
fn key(&self) -> Self::Key {
Key::from(self.id)
}
}
fn get<T: Record>(_key: T::Key) {}
fn get2<T: Record, K: Into<T::Key>>(_key: K) -> Option<T> {
None
}
#[test]
fn test_that_are_key_type_is_useful() {
let a = Key::<u8> { 0: 0 };
assert_eq!(0, a.0);
let b = Key::from(8);
assert_eq!(8, b.0);
let c = Key::from("Yo");
assert_eq!("Yo", c.0);
let d = Key::from(String::from("LOL"));
assert_eq!("LOL".to_string(), d.0);
let _e = get::<Thing>(Key::from(8));
let _f = get::<OtherThing>(String::from("LOL"));
let g = Key::from([0, 1, 2, 3]);
assert_eq!([0, 1, 2, 3], g.0);
let _h: Option<Thing> = get2(8);
let _i: Option<OtherThing> = get2("Hello".to_string());
let _j: Option<OtherThing> = get2("Hi");
}
}