#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

#12 in #drop

Download history 59/week @ 2024-07-30 107/week @ 2024-08-06 129/week @ 2024-08-13 177/week @ 2024-08-20 152/week @ 2024-08-27 55/week @ 2024-09-03 61/week @ 2024-09-10 78/week @ 2024-09-17 427/week @ 2024-09-24 1136/week @ 2024-10-01 785/week @ 2024-10-08 1481/week @ 2024-10-15 974/week @ 2024-10-22 674/week @ 2024-10-29 372/week @ 2024-11-05 729/week @ 2024-11-12

3,029 downloads per month
Used in 17 crates (3 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