Skip to content

Commit

Permalink
feat: add mutex_cache.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav-rama committed Mar 22, 2024
1 parent e639952 commit 250ade4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/bin/mutex_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use tokio::sync::Mutex;

struct Cache<T>(Mutex<Vec<T>>);

impl<T> Cache<T> {
async fn get(&self, index: usize) -> Option<T>
where
T: Clone,
{
self.0.lock().await.get(index).cloned()
}
}

#[tokio::main]
async fn main() {
let cache = Cache(Mutex::new(vec![
"Rust ".to_string(),
"is ".to_string(),
"awesome!".to_string(),
]));

for i in 0..3 {
if let Some(item) = cache.get(i).await {
print!("{}", item);
}
}
}

// Rust is awesome!

0 comments on commit 250ade4

Please sign in to comment.