https://github.com/zeozeozeo/type-handle
Regular and reference-counted type handles for Rust
https://github.com/zeozeozeo/type-handle
ffi ffi-helpers handle memory-management native rchandle reference-counting rust-ffi rust-helper rust-patterns rustlang tiny types utils
Last synced: 3 months ago
JSON representation
Regular and reference-counted type handles for Rust
- Host: GitHub
- URL: https://github.com/zeozeozeo/type-handle
- Owner: zeozeozeo
- License: unlicense
- Created: 2023-08-29T19:57:40.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-08-31T09:37:48.000Z (almost 2 years ago)
- Last Synced: 2025-02-28T08:53:46.483Z (4 months ago)
- Topics: ffi, ffi-helpers, handle, memory-management, native, rchandle, reference-counting, rust-ffi, rust-helper, rust-patterns, rustlang, tiny, types, utils
- Language: Rust
- Homepage: https://docs.rs/type-handle
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# type-handle
Tiny Rust library that exports `Handle` and `RCHandle`. Can be useful for wrapping native ffi structs/pointers.
Both `Handle` and `RCHandle` implement `Clone`, where `Handle` will clone the underlying struct instance (if it implements `Clone`), and `RCHandle` will keep the underlying pointer.
`Handle` and `RCHandle` implement `Send`/`Sync` by default with the `send_sync` feature.
They both implement `Deref` and `DerefMut`, so you can access a field through a handle the same as you normally would on a normal instance.
# Example
## `Handle`
```rust
#[derive(Clone)]
struct Animal {
is_dog: bool,
}let animal = Animal { is_dog: false };
let cat = Handle::from_instance(animal);// clone `cat` and mutate `is_dog`, note that `animal` is not mutable
let dog = handle.clone(); // this clones `Animal`, `Animal` must implement `Clone`
dog.is_dog = true;
```## `RCHandle` (reference-counted handle)
```rust
// don't have to #[derive(Clone)] here!
struct Animal {
is_dog: bool,
}let mut animal = Animal { is_dog: false };
let mut handle = RCHandle::from_ptr(&mut animal);// note that `Animal` does not implement `Clone`, because
// cloning an `RCHandle` does not clone the underlying type
let mut handle2 = handle.clone();handle2.is_dog = true;
assert!(handle.is_dog == handle2.is_dog);
```# Tests
To run tests, run `cargo test`.
# License
Public domain (unlicense).