Skip to main content

Crate handle_trait

Crate handle_trait 

Source
Expand description

The Handle trait for types that represent handles to shared resources.

This crate provides the Handle trait, which identifies types that function as handles to underlying resources. A handle’s defining feature is that cloning it results in a second value that accesses the same underlying resource, creating what can be thought of as “entanglement”—modifications visible through one handle appear in all other handles to the same resource.

§Examples

use handle_trait::Handle;
use std::rc::Rc;
use std::cell::RefCell;

let data = Rc::new(RefCell::new(42));
let handle1 = data.handle(); // More semantically clear than .clone()

*data.borrow_mut() = 100;
assert_eq!(*handle1.borrow(), 100); // Both handles see the same value

§Derive Macro

You can derive Handle for your own types:

use handle_trait::Handle;
use std::sync::Arc;

#[derive(Clone, Handle)]
struct MyHandle {
    inner: Arc<i32>,
}

let h1 = MyHandle { inner: Arc::new(42) };
let h2 = h1.handle();

Traits§

Handle
Indicates that this type is a handle to some underlying resource.

Derive Macros§

Handle
Derive macro for the Handle trait.