pub struct Tracker<C>(/* private fields */);
Expand description
An object used to keep track of controller parts of object-controller pairs.
Implementations§
Source§impl<C> Tracker<C>
impl<C> Tracker<C>
Sourcepub fn new() -> Tracker<C>
pub fn new() -> Tracker<C>
Create a new tracker.
Examples found in repository?
examples/example2.rs (line 52)
51fn main() {
52 let mut tracker = Tracker::new();
53
54 let mut threads = Vec::new();
55
56 for i in 1..5 {
57 let tracker2 = tracker.clone();
58 let thread = std::thread::spawn(move || {
59 let (object, controller) = Object::new2(i);
60 let object = tracker2.track(object, controller);
61
62 object.do_something();
63
64 println!("Quit {}", i);
65 });
66 threads.push(thread);
67 }
68
69 std::thread::sleep(Duration::from_secs(5));
70
71 tracker.for_each(|r| r.cancel());
72
73 for thread in threads {
74 thread.join().unwrap();
75 }
76}
More examples
examples/example1.rs (line 42)
40fn main() {
41 // Create the tracker object.
42 let mut tracker = Tracker::new();
43
44 let tracker2 = tracker.clone();
45 let thread = std::thread::spawn(move || {
46 // Create an Object-Controller pair in some thread.
47 let (object, controller) = Object::new2();
48
49 // Register it with the tracker.
50 let object = tracker2.track(object, controller);
51
52 // Do some work with the Object.
53 object.do_something();
54 });
55
56 std::thread::sleep(Duration::from_secs(5));
57
58 // Cancel all registered Object operations.
59 tracker.for_each(|r| r.cancel());
60
61 thread.join().unwrap();
62}
Sourcepub fn track<T>(&self, object: T, controller: C) -> Tracked<T, C>
pub fn track<T>(&self, object: T, controller: C) -> Tracked<T, C>
Register an object-controller pair.
The controller part is kept in the Tracker, and the object part is wrapped in a Tracked. When the Tracked object is dropped, the controller is dropped too.
Examples found in repository?
examples/example2.rs (line 60)
51fn main() {
52 let mut tracker = Tracker::new();
53
54 let mut threads = Vec::new();
55
56 for i in 1..5 {
57 let tracker2 = tracker.clone();
58 let thread = std::thread::spawn(move || {
59 let (object, controller) = Object::new2(i);
60 let object = tracker2.track(object, controller);
61
62 object.do_something();
63
64 println!("Quit {}", i);
65 });
66 threads.push(thread);
67 }
68
69 std::thread::sleep(Duration::from_secs(5));
70
71 tracker.for_each(|r| r.cancel());
72
73 for thread in threads {
74 thread.join().unwrap();
75 }
76}
More examples
examples/example1.rs (line 50)
40fn main() {
41 // Create the tracker object.
42 let mut tracker = Tracker::new();
43
44 let tracker2 = tracker.clone();
45 let thread = std::thread::spawn(move || {
46 // Create an Object-Controller pair in some thread.
47 let (object, controller) = Object::new2();
48
49 // Register it with the tracker.
50 let object = tracker2.track(object, controller);
51
52 // Do some work with the Object.
53 object.do_something();
54 });
55
56 std::thread::sleep(Duration::from_secs(5));
57
58 // Cancel all registered Object operations.
59 tracker.for_each(|r| r.cancel());
60
61 thread.join().unwrap();
62}
Sourcepub fn track_pair<T>(&self, pair: (T, C)) -> Tracked<T, C>
pub fn track_pair<T>(&self, pair: (T, C)) -> Tracked<T, C>
Register an object-contoller pair.
Same as track(pair.0, pair.1).
Sourcepub fn lock(&mut self) -> TrackerGuard<'_, C>
pub fn lock(&mut self) -> TrackerGuard<'_, C>
Lock the tracker so that one can iterate over its controllers.
Sourcepub fn for_each<F: FnMut(&C)>(&mut self, f: F)
pub fn for_each<F: FnMut(&C)>(&mut self, f: F)
Call a closure on each tracked controller. The closure must not register a new controller in this tracker, or drop a tracked object. The controllers are visited in an unspecified order.
Examples found in repository?
examples/example2.rs (line 71)
51fn main() {
52 let mut tracker = Tracker::new();
53
54 let mut threads = Vec::new();
55
56 for i in 1..5 {
57 let tracker2 = tracker.clone();
58 let thread = std::thread::spawn(move || {
59 let (object, controller) = Object::new2(i);
60 let object = tracker2.track(object, controller);
61
62 object.do_something();
63
64 println!("Quit {}", i);
65 });
66 threads.push(thread);
67 }
68
69 std::thread::sleep(Duration::from_secs(5));
70
71 tracker.for_each(|r| r.cancel());
72
73 for thread in threads {
74 thread.join().unwrap();
75 }
76}
More examples
examples/example1.rs (line 59)
40fn main() {
41 // Create the tracker object.
42 let mut tracker = Tracker::new();
43
44 let tracker2 = tracker.clone();
45 let thread = std::thread::spawn(move || {
46 // Create an Object-Controller pair in some thread.
47 let (object, controller) = Object::new2();
48
49 // Register it with the tracker.
50 let object = tracker2.track(object, controller);
51
52 // Do some work with the Object.
53 object.do_something();
54 });
55
56 std::thread::sleep(Duration::from_secs(5));
57
58 // Cancel all registered Object operations.
59 tracker.for_each(|r| r.cancel());
60
61 thread.join().unwrap();
62}
Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for Tracker<C>
impl<C> RefUnwindSafe for Tracker<C>
impl<C> Send for Tracker<C>where
C: Send,
impl<C> Sync for Tracker<C>where
C: Send,
impl<C> Unpin for Tracker<C>
impl<C> UnwindSafe for Tracker<C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more