Giter Site home page Giter Site logo

zeozeozeo / type-handle Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 5 KB

Regular and reference-counted type handles for Rust

Home Page: https://docs.rs/type-handle

License: The Unlicense

Rust 100.00%
handle memory-management reference-counting rust-helper rust-patterns rustlang tiny types utils rchandle

type-handle's Introduction

type-handle

Tiny Rust library that exports Handle<T> and RCHandle<T>. 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<T>

#[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<T> (reference-counted handle)

// 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).

type-handle's People

Contributors

zeozeozeo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.