From f4ddfbb2aa8dd640ccd8a6572b2a636bde7b3ef1 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 6 Apr 2022 11:28:34 -0300 Subject: [PATCH] Simple test for RwLock --- rwlocktest/Cargo.lock | 7 +++++++ rwlocktest/Cargo.toml | 8 ++++++++ rwlocktest/src/main.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 rwlocktest/Cargo.lock create mode 100644 rwlocktest/Cargo.toml create mode 100644 rwlocktest/src/main.rs diff --git a/rwlocktest/Cargo.lock b/rwlocktest/Cargo.lock new file mode 100644 index 0000000..8ecf37c --- /dev/null +++ b/rwlocktest/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rwlocktest" +version = "0.1.0" diff --git a/rwlocktest/Cargo.toml b/rwlocktest/Cargo.toml new file mode 100644 index 0000000..71531e2 --- /dev/null +++ b/rwlocktest/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rwlocktest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rwlocktest/src/main.rs b/rwlocktest/src/main.rs new file mode 100644 index 0000000..3dc7ed0 --- /dev/null +++ b/rwlocktest/src/main.rs @@ -0,0 +1,31 @@ +use std::sync::RwLock; + +struct Holder { + content: RwLock, +} + +impl Holder { + fn new() -> Self { + Self { + content: RwLock::new(String::from("hello")), + } + } + + fn change(&self, content: &str) { + let mut original = self.content.write().unwrap(); + *original = content.into(); + } + + fn content(&self) -> String { + let lock = self.content.read().unwrap(); + String::from(&*lock) + } +} + +fn main() { + let content = Holder::new(); + println!("Content: {}", content.content()); + + content.change("Hello there"); + println!("Content: {}", content.content()); +}