Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[macro_use]
- extern crate lazy_static;
- use std::collections::HashMap;
- use std::sync::Mutex;
- use std::fmt::Display;
- trait Value: Send + Display {
- fn box_clone(&self) -> Box<dyn Value>;
- }
- /*impl Value for isize {
- fn box_clone(&self) -> Box<dyn Value> {
- Box::new((*self).clone())
- }
- }
- impl Value for String {
- fn box_clone(&self) -> Box<dyn Value> {
- Box::new((*self).clone())
- }
- }*/
- impl<T: 'static + Send + Clone + Display> Value for T {
- fn box_clone(&self) -> Box<dyn Value> {
- Box::new((*self).clone())
- }
- }
- #[derive(Clone)]
- struct S {
- value: Box<dyn Value>
- }
- impl Clone for Box<dyn Value> {
- fn clone(&self) -> Box<dyn Value> {
- self.box_clone()
- }
- }
- lazy_static! {
- static ref REGISTRY: Mutex<HashMap<String, S>> = {
- Mutex::new(HashMap::new())
- };
- }
- impl REGISTRY {
- fn get(&self, key: &str) -> Option<S> {
- self.lock().unwrap().get(&String::from(key)).map(|s| s.clone())
- }
- fn set(&self, key: &str, value: S) -> Option<S> {
- self.lock().unwrap().insert(String::from(key), value)
- }
- }
- fn main() {
- REGISTRY.set("foo", S { value: Box::new(String::from("hello world")) });
- REGISTRY.set("bar", S { value: Box::new(123) });
- println!("{}", REGISTRY.get("foo").unwrap().value);
- println!("{}", REGISTRY.get("bar").unwrap().value);
- }
Add Comment
Please, Sign In to add comment