소스 검색

Renamed from Shard to Shared

The data is being Shared, not Shard

This also loads the kitchen sink from tokio, just so we can have a working example in Shared::lock
david 2 달 전
부모
커밋
6bc45b22f3
3개의 변경된 파일118개의 추가작업 그리고 9개의 파일을 삭제
  1. 48 1
      Cargo.lock
  2. 2 2
      Cargo.toml
  3. 68 6
      src/lib.rs

+ 48 - 1
Cargo.lock

@@ -89,6 +89,24 @@ version = "0.2.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
 
+[[package]]
+name = "proc-macro2"
+version = "1.0.86"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.37"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
+dependencies = [
+ "proc-macro2",
+]
+
 [[package]]
 name = "rustc-demangle"
 version = "0.1.24"
@@ -96,7 +114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
 
 [[package]]
-name = "shard"
+name = "shared"
 version = "0.1.0"
 dependencies = [
  "tokio",
@@ -108,6 +126,17 @@ version = "1.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
 
+[[package]]
+name = "syn"
+version = "2.0.76"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
 [[package]]
 name = "tokio"
 version = "1.40.0"
@@ -116,4 +145,22 @@ checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998"
 dependencies = [
  "backtrace",
  "pin-project-lite",
+ "tokio-macros",
 ]
+
+[[package]]
+name = "tokio-macros"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"

+ 2 - 2
Cargo.toml

@@ -1,7 +1,7 @@
 [package]
-name = "shard"
+name = "shared"
 version = "0.1.0"
 edition = "2021"
 
 [dependencies]
-tokio = { version = "1.40.0", features = ["sync", "time"] }
+tokio = { version = "1.40.0", features = ["macros", "rt", "rt-multi-thread", "sync", "time"] }

+ 68 - 6
src/lib.rs

@@ -6,13 +6,13 @@ use tokio::time::{timeout, Duration};
 const LOCK_TIMEOUT: u64 = 30;
 
 #[derive(Clone)]
-pub struct Shard<T> {
+pub struct Shared<T> {
     pub data: Arc<Mutex<T>>,
 }
 
-impl<T> Shard<T> {
+impl<T> Shared<T> {
     pub fn new(data: T) -> Self {
-        Shard {
+        Shared {
             data: Arc::new(Mutex::new(data)),
         }
     }
@@ -21,9 +21,71 @@ impl<T> Shard<T> {
     ///
     /// 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> {