// cooper-rs/examples/shared_keyval.rs // // This is an example app for the Rust "cooper" library. // // Copyright (c) 2021, Frank Pagliughi // All Rights Reserved // // Licensed under the MIT license: // // This file may not be copied, modified, or distributed except according // to those terms. // use std::collections::HashMap; use cooper::Actor; /// The internal state type for the Actor type State = HashMap; /// An actor that can act as a shared key/value store of strings. #[derive(Clone)] pub struct SharedMap { actor: Actor, } impl SharedMap { /// Create a new actor to share a key/value map of string. pub fn new() -> Self { Self { actor: Actor::new() } } /// Insert a value into the shared map. pub async fn insert(&self, key: K, val: V) where K: Into, V: Into, { let key = key.into(); let val = val.into(); self.actor.cast(|state| Box::pin(async move { state.insert(key, val); })).await } /// Gets the value, if any, from the shared map that is /// associated with the key. pub async fn get(&self, key: K) -> Option where K: Into, { let key = key.into(); self.actor.call(|state| Box::pin(async move { state.get(&key).map(|v| v.to_string()) })).await } } // -------------------------------------------------------------------------- fn main() { let map = SharedMap::new(); let h = smol::spawn(async move { println!("Inserting entry 'city'..."); map.insert("city", "Boston").await; println!("Retrieving entry..."); match map.get("city").await { Some(s) => println!("Got: {}", s), None => println!("Error: No entry found"), } }); smol::block_on(h); }