Struct Tracker

Source
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>

Source

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
Hide additional 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}
Source

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
Hide additional 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}
Source

pub fn track_pair<T>(&self, pair: (T, C)) -> Tracked<T, C>

Register an object-contoller pair.

Same as track(pair.0, pair.1).

Source

pub fn lock(&mut self) -> TrackerGuard<'_, C>

Lock the tracker so that one can iterate over its controllers.

Source

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
Hide additional 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§

Source§

impl<C> Clone for Tracker<C>

Source§

fn clone(&self) -> Tracker<C>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C> Default for Tracker<C>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.