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.
- 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 createObject
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.
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
.
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"));
dynamic_object
does not use unsafe
code.
dynamic_object
is licensed under the MIT license. Please see the LICENSE
file for more information.
Contributions to are welcome! Please open an issue or submit a pull request.
If you have any questions or feedback, please feel free to open an issue. You can also reach out to me on Xitter.