GithubHelp home page GithubHelp logo

h2co3 / dynamic_object Goto Github PK

View Code? Open in Web Editor NEW

This project forked from trvswgnr/dynamic_object

0.0 1.0 0.0 12 KB

Dynamic, Type-Erased Key-Value Maps in Rust

License: MIT License

Rust 100.00%

dynamic_object's Introduction

dynamic_object: Dynamic, Type-Erased Key-Value Maps in Rust

Do you love Rust but are tired of being constrained by static typing when you need a map to hold values of different types? Do you wish you could have the flexibility of JavaScript objects in Rust? Look no further! dynamic_object is here to save the day!

dynamic_object is a Rust crate that provides an easy way to create dynamic, type-erased, key-value maps. It allows you to store any value that implements the Any trait and retrieve it with type checking at runtime. This is particularly useful when you need a map to hold values of different types and you can't determine the types at compile time.

Features

  • Dynamic key-value map: Store any value that implements the Any trait.
  • Type checking at runtime: Retrieve your values with type checking at runtime.
  • Macro for easy and intuitive object creation: Use the object macro to create Object instances in a way that feels natural and intuitive.

Usage

Add dynamic_object to your Cargo.toml:

[dependencies]
dynamic_object = { git = "https://github.com/trvswgnr/dynamic_object" }

You can create an Object and insert any type that implements Any:

use dynamic_object::Object;
let mut object = Object::new();
object.insert("key", "value");

You can retrieve a reference to the original value if it is of the correct type:

use dynamic_object::Object;
let mut object = Object::new();
object.insert("key", "value");
let value = object.get_as::<&str>("key");
assert_eq!(value, Some(&"value"));

If it isn't of the correct type, you will get None.

Object Macro

The object macro provided by this crate makes it easy to create Object instances. It supports nested objects.

use dynamic_object::object;
let obj = object!({
    key1: "value1",
    key2: {
        inner_key: "inner_value",
    },
});
assert_eq!(obj.get_as::<&str>("key1"), Some(&"value1"));

Safety

The AnyType struct uses Box::leak to create a 'static reference, which means the value will live for the entire duration of the program. Therefore, it's safe to use and doesn't require the user to manage the lifetime of the value.

License

dynamic_object is licensed under the MIT license. Please see the LICENSE file in the GitHub repository for more information.

Contributing

Contributions to are welcome! Please open an issue or submit a pull request.

Contact

If you have any questions or feedback, please feel free to open an issue. You can also reach out to me on Xitter.

dynamic_object's People

Contributors

h2co3 avatar trvswgnr 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.