Git Product home page Git Product logo

dynamic_object's Introduction

Dynamic Objects 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 simple syntax and flexibility of JavaScript objects in Rust? Look no further - dynamic_object is here to save the day!

The dynamic_object crate 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.
  • Nested objects: The object macro supports nested objects.
  • Order preservation: The Object struct preserves the order of insertion.

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 also 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

dynamic_object does not use unsafe code.

License

dynamic_object is licensed under the MIT license. Please see the LICENSE file 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

trvswgnr avatar h2co3 avatar

Stargazers

Devon 'fire' Adkisson avatar Paul Valladares avatar Glitchbyte avatar Shreyas Mididoddi avatar John Wass avatar Patrick G avatar Alex avatar Ahmad Sandid avatar Ashwin Vinod avatar  avatar Robb Currall avatar  avatar Luke Hedger avatar

Watchers

 avatar

Forkers

h2co3

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.