|
@@ -6,13 +6,13 @@ use tokio::time::{timeout, Duration};
|
|
const LOCK_TIMEOUT: u64 = 30;
|
|
const LOCK_TIMEOUT: u64 = 30;
|
|
|
|
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
-pub struct Shard<T> {
|
|
|
|
|
|
+pub struct Shared<T> {
|
|
pub data: Arc<Mutex<T>>,
|
|
pub data: Arc<Mutex<T>>,
|
|
}
|
|
}
|
|
|
|
|
|
-impl<T> Shard<T> {
|
|
|
|
|
|
+impl<T> Shared<T> {
|
|
pub fn new(data: T) -> Self {
|
|
pub fn new(data: T) -> Self {
|
|
- Shard {
|
|
|
|
|
|
+ Shared {
|
|
data: Arc::new(Mutex::new(data)),
|
|
data: Arc::new(Mutex::new(data)),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -21,9 +21,71 @@ impl<T> Shard<T> {
|
|
///
|
|
///
|
|
/// Using:
|
|
/// Using:
|
|
/// ```
|
|
/// ```
|
|
- /// {
|
|
|
|
- /// let data = shared.lock().await;
|
|
|
|
- /// // make changes to T
|
|
|
|
|
|
+ /// use shared::Shared;
|
|
|
|
+ ///
|
|
|
|
+ /// #[derive(Clone)]
|
|
|
|
+ /// struct Post {
|
|
|
|
+ /// pub id: i32,
|
|
|
|
+ /// pub title: String,
|
|
|
|
+ /// pub author: String,
|
|
|
|
+ /// }
|
|
|
|
+ ///
|
|
|
|
+ /// #[derive(Clone)]
|
|
|
|
+ /// struct ExampleData {
|
|
|
|
+ /// pub name: String,
|
|
|
|
+ /// pub age: u16,
|
|
|
|
+ /// pub last_post: Shared<Post>
|
|
|
|
+ /// }
|
|
|
|
+ ///
|
|
|
|
+ /// async fn do_something(shared: Shared<ExampleData>) {
|
|
|
|
+ /// let mut example = shared.lock().await;
|
|
|
|
+ /// example.age += 1;
|
|
|
|
+ /// {
|
|
|
|
+ /// let mut post = example.last_post.lock().await;
|
|
|
|
+ /// post.id += 1;
|
|
|
|
+ /// post.title = "Sharing Data in Rust, It's Easy".to_string();
|
|
|
|
+ /// }
|
|
|
|
+ /// }
|
|
|
|
+ ///
|
|
|
|
+ /// #[tokio::main]
|
|
|
|
+ /// async fn main() {
|
|
|
|
+ /// let ex_data = Shared::new(ExampleData{
|
|
|
|
+ /// name: "Test Dummy".to_string(),
|
|
|
|
+ /// age: 30,
|
|
|
|
+ /// last_post: Shared::new(Post{
|
|
|
|
+ /// id: 10,
|
|
|
|
+ /// title: "Sharing Data in Rust, The Easy Way".to_string(),
|
|
|
|
+ /// author: "Test Dummy".to_string(),
|
|
|
|
+ /// })
|
|
|
|
+ /// });
|
|
|
|
+ ///
|
|
|
|
+ /// {
|
|
|
|
+ /// let example = ex_data.lock().await;
|
|
|
|
+ /// println!("Before:");
|
|
|
|
+ /// println!("name: {}", example.name);
|
|
|
|
+ /// println!("age: {}", example.age);
|
|
|
|
+ /// {
|
|
|
|
+ /// let post = example.last_post.lock().await;
|
|
|
|
+ /// println!("last_post.id: {}", post.id);
|
|
|
|
+ /// println!("last_post.title: {}", post.title);
|
|
|
|
+ /// println!("last_post.author: {}", post.author);
|
|
|
|
+ /// }
|
|
|
|
+ /// }
|
|
|
|
+ ///
|
|
|
|
+ /// do_something(ex_data.clone()).await;
|
|
|
|
+ ///
|
|
|
|
+ /// {
|
|
|
|
+ /// let example = ex_data.lock().await;
|
|
|
|
+ /// println!("Before:");
|
|
|
|
+ /// println!("name: {}", example.name);
|
|
|
|
+ /// println!("age: {}", example.age);
|
|
|
|
+ /// {
|
|
|
|
+ /// let post = example.last_post.lock().await;
|
|
|
|
+ /// println!("last_post.id: {}", post.id);
|
|
|
|
+ /// println!("last_post.title: {}", post.title);
|
|
|
|
+ /// println!("last_post.author: {}", post.author);
|
|
|
|
+ /// }
|
|
|
|
+ /// }
|
|
/// }
|
|
/// }
|
|
/// ```
|
|
/// ```
|
|
pub async fn lock(&self) -> MutexGuard<'_, T> {
|
|
pub async fn lock(&self) -> MutexGuard<'_, T> {
|