diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-10-31 03:54:43 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-10-31 03:54:43 +0300 |
| commit | 62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2 (patch) | |
| tree | b8461eda6cce3f9bbfb2261dd828077e17c7d226 /src/interior_mutability | |
| parent | 18ad654357d6cca34782aaa269f5e8e06cbe9241 (diff) | |
| download | rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.gz rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.bz2 rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.zip | |
add: 7 craits
smartpointers
interior_mutability
threads
mutex
hello-async
async_concurrency
multi_futures
Diffstat (limited to 'src/interior_mutability')
| -rw-r--r-- | src/interior_mutability/Cargo.toml | 6 | ||||
| -rw-r--r-- | src/interior_mutability/src/lib.rs | 72 | ||||
| -rw-r--r-- | src/interior_mutability/src/main.rs | 73 |
3 files changed, 151 insertions, 0 deletions
diff --git a/src/interior_mutability/Cargo.toml b/src/interior_mutability/Cargo.toml new file mode 100644 index 0000000..5167438 --- /dev/null +++ b/src/interior_mutability/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "interior_mutability" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/interior_mutability/src/lib.rs b/src/interior_mutability/src/lib.rs new file mode 100644 index 0000000..c854bb1 --- /dev/null +++ b/src/interior_mutability/src/lib.rs @@ -0,0 +1,72 @@ +pub trait Messenger { + fn send(&self, msg: &str); +} + +pub struct LimitTracker<'a, T: Messenger> { + messenger: &'a T, + value: usize, + max: usize, +} + +impl<'a, T> LimitTracker<'a, T> +where + T: Messenger, +{ + pub fn new(messenger: &'a T, max: usize) -> LimitTracker<'a, T> { + LimitTracker { + messenger, + value: 0, + max, + } + } + + pub fn set_value(&mut self, value: usize) { + self.value = value; + + let percentage_of_max = self.value as f64 / self.max as f64; + + if percentage_of_max >= 1.0 { + self.messenger.send("Error: You are over your quota!"); + } else if percentage_of_max >= 0.9 { + self.messenger + .send("Urgent warning: You've used up over 90% of your quota!"); + } else if percentage_of_max >= 0.75 { + self.messenger + .send("Warning: You've used up over 75% of your quota!"); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::cell::RefCell; + + struct MockMessenger { + sent_messages: RefCell<Vec<String>>, + } + + impl MockMessenger { + fn new() -> MockMessenger { + MockMessenger { + sent_messages: RefCell::new(vec![]), + } + } + } + + impl Messenger for MockMessenger { + fn send(&self, message: &str) { + self.sent_messages.borrow_mut().push(String::from(message)); + } + } + + #[test] + fn it_sends_an_over_75_percent_warning_message() { + let mock_messenger = MockMessenger::new(); + let mut limit_tracker = LimitTracker::new(&mock_messenger, 100); + + limit_tracker.set_value(80); + + assert_eq!(mock_messenger.sent_messages.borrow().len(), 1); + } +} diff --git a/src/interior_mutability/src/main.rs b/src/interior_mutability/src/main.rs new file mode 100644 index 0000000..019a4ac --- /dev/null +++ b/src/interior_mutability/src/main.rs @@ -0,0 +1,73 @@ +use crate::List::{Cons, Nil}; +use std::cell::RefCell; +use std::rc::{Rc, Weak}; + +#[derive(Debug)] +struct Node { + value: i32, + parent: RefCell<Weak<Node>>, + children: RefCell<Vec<Rc<Node>>>, +} + +#[derive(Debug)] +enum List { + Cons(Rc<RefCell<i32>>, Rc<List>), + Nil, +} + +fn main() { + let value = Rc::new(RefCell::new(5)); + + let a = Rc::new(Cons(Rc::clone(&value), Rc::new(Nil))); + + let b = Cons(Rc::new(RefCell::new(3)), Rc::clone(&a)); + let c = Cons(Rc::new(RefCell::new(4)), Rc::clone(&a)); + + *value.borrow_mut() += 10; + + println!("a after = {a:?}"); + println!("b after = {b:?}"); + println!("c after = {c:?}"); + + let leaf = Rc::new(Node { + value: 3, + parent: RefCell::new(Weak::new()), + children: RefCell::new(vec![]), + }); + + println!( + "leaf strong = {}, weak = {}", + Rc::strong_count(&leaf), + Rc::weak_count(&leaf), + ); + + { + let branch = Rc::new(Node { + value: 5, + parent: RefCell::new(Weak::new()), + children: RefCell::new(vec![Rc::clone(&leaf)]), + }); + + *leaf.parent.borrow_mut() = Rc::downgrade(&branch); + + println!( + "branch strong = {}, weak = {}", + Rc::strong_count(&branch), + Rc::weak_count(&branch), + ); + + println!( + "leaf strong = {}, weak = {}", + Rc::strong_count(&leaf), + Rc::weak_count(&leaf), + ); + } + + println!("leaf parent = {:?}", leaf.parent.borrow().upgrade()); + + println!( + "leaf strong = {}, weak = {}", + Rc::strong_count(&leaf), + Rc::weak_count(&leaf), + ); +} |
