1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#![allow(unused_imports)]
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
use crate::prelude::BuildWith;
use super::Id;
// println!("uniq i8: {:?}", Key::with(-32 as i8));
// println!("uniq i16: {:?}", Key::with(-5235 as i16));
// println!("uniq i32: {:?}", Key::with(-23512 as i32));
// println!("uniq i64: {:?}", Key::with(-235123 as i64));
// println!("uniq u8: {:?}", Key::with(32 as u8));
// println!("uniq u16: {:?}", Key::with(5235 as u16));
// println!("uniq u32: {:?}", Key::with(23512 as u32));
// println!("uniq u64: {:?}", Key::with(235123 as u64));
// println!("uniq usize: {:?}", Key::with(235123 as usize));
// println!("uniq: {:?}", Key::with("my str key"));
// println!("uniq: {:?}", Key::with(String::from("from String")));
// let base = Key::default();
// println!("BASE: {:?}", base);
// println!("CHILD: {:?}", Key::with(&base));
// let mut hasher = DefaultHasher::new();
// base.hash(&mut hasher);
// println!("Hash is {}", hasher.finish());
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Key(String);
impl Key {
pub fn id(&self) -> Id {
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
Id(hasher.finish())
}
}
// Creates a unique key whose value does not matter. .
// Can be used in the last mile and as shadowed unique keys
// or as keys that don't matter.
//
impl Default for Key {
fn default() -> Self {
Self(format!("default::{:020}", rand::random::<u64>()))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<i8> for Key {
fn with(param: i8) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<i16> for Key {
fn with(param: i16) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<i32> for Key {
fn with(param: i32) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<i64> for Key {
fn with(param: i64) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<u8> for Key {
fn with(param: u8) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<u16> for Key {
fn with(param: u16) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<u32> for Key {
fn with(param: u32) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<u64> for Key {
fn with(param: u64) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<usize> for Key {
fn with(param: usize) -> Self {
Self(format!("{:020}", param))
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<&str> for Key {
fn with(param: &str) -> Self {
Self(param.to_owned())
}
}
// Create a key with a parameter value to use later.
// The developer must make sure that the keys are unique
impl BuildWith<String> for Key {
fn with(param: String) -> Self {
Self(param)
}
}
// Creates a unique key based on the parent key.
// Here we create a key value in the "base::new" format
// to create a unique key from the root of the application.
// In other words, we are setting a parent for the new key.
//
// Therefore, when you are developing reusable composite components,
// it will be convenient to hide the child components and generate a
// unique identifier for them from the keypath.
//
impl BuildWith<Key> for Key {
fn with(param: Key) -> Self {
Self(format!("{}::{:020}", ¶m.0, rand::random::<u64>()))
}
}
impl BuildWith<&Key> for Key {
fn with(param: &Key) -> Self {
Self(format!("{}::{:020}", ¶m.0, rand::random::<u64>()))
}
}