#object #id #drop

object-id

Unique object ID with no generation

5 releases

0.1.4 Mar 3, 2024
0.1.3 Mar 3, 2024
0.1.2 Mar 3, 2024
0.1.1 Mar 3, 2024
0.1.0 Mar 3, 2024

#668 in Development tools

Download history 183/week @ 2024-03-03 189/week @ 2024-03-10 31/week @ 2024-03-17 97/week @ 2024-03-24 250/week @ 2024-03-31 185/week @ 2024-04-07 480/week @ 2024-04-14 923/week @ 2024-04-21

1,838 downloads per month
Used in 6 crates (2 directly)

MIT license

7KB
119 lines

object-id

Rust unique object ID

A simple object which guaranties that object ID is unique until dropped.

How it works

The UniqueId object does not generate any IDs itself, instead it allocates a single byte in the heap. The pointer to the byte can be used to identify an object itself and any other object which holds the ID one.

Why it is useful

Unique object IDs are used (usually by Drop) to notify various 3rd party objects that the source is dropped and e.g. must be unsubscribed from events, removed from collections, maps etc.

Cloning

Keep in mind that when either UniqueId object or the parent object is cloned, the ID is changed.

Other traits

UniqueId implements the majority of traits the parent object may demand.

How to use

As the UniqueId object is changed when cloned, no matter solo or with the parent object, the target collections should keep / compare it numeric representation instead. Example:

use object_id::UniqueId;
use once_cell::sync::Lazy;
use std::sync::mpsc::{self, Sender, Receiver};
use std::sync::Mutex;

static EVENT_RECEIVERS: Lazy<Mutex<Vec<(usize, Sender<()>)>>> = Lazy::new(<_>::default);

struct Client {
    id: UniqueId,
    rx: Receiver<()>
}

impl Drop for Client {
    fn drop(&mut self) {
        EVENT_RECEIVERS.lock().unwrap().retain(|(id, _)| *id != self.id.as_usize());
    }
}

{
    let (tx, rx) = mpsc::channel();
    let client = Client { id: <_>::default(), rx };
    EVENT_RECEIVERS.lock().unwrap().push((client.id.as_usize(), tx));
} // the client is dropped here
assert!(EVENT_RECEIVERS.lock().unwrap().is_empty());

The similar method can be used to store an async Waker of an object together with its unique ID to notify the producer that the object Future is dropped before data is consumed and properly handle aborts.

No runtime deps