Serializable map of any type.

Overview

🗂️ type_reg

Crates.io docs.rs CI Coverage Status

Serializable map of any type.

This library provides a map that can store any serializable type, and retrieve it as the strong type. Serialization and deserialization is also done without requiring the consumer to implement custom serde logic.

Usage

Add the following to Cargo.toml

type_reg = { version = "0.3.0", features = ["tagged"] }
type_reg = { version = "0.3.0", features = ["untagged"] }

# Values must impl Debug, and TypeMap's Debug impl will
# print the debug string of each value.
type_reg = { version = "0.3.0", features = ["debug"] }

# Use insertion order for TypeMap and TypeReg iteration order.
type_reg = { version = "0.3.0", features = ["ordered"] }

Tagged Type Registry

⚠️ Note: This uses std::any::type_name internally, which is not stable.

Serialization

use serde::{Deserialize, Serialize};
use type_reg::tagged::TypeMap;

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct A(u32);

fn main() {
    let mut type_map = TypeMap::new();
    type_map.insert("one", 1u32);
    type_map.insert("two", 2u64);
    type_map.insert("three", A(3));

    println!("{}", serde_yaml::to_string(&type_map).unwrap());

    // ---
    // one:
    //   u32: 1
    // three:
    //   "rust_out::A": 3
    // two:
    //   u64: 2
}

Deserialization

= type_reg.deserialize_map(deserializer).unwrap(); let data_u32 = type_map.get::("one").copied().unwrap(); let data_u64 = type_map.get::("two").copied().unwrap(); let data_a = type_map.get::("three").copied().unwrap(); println!("{data_u32}, {data_u64}, {data_a:?}"); // 1, 2, A(3) }">
use serde::{Deserialize, Serialize};
use type_reg::tagged::{TypeMap, TypeReg};

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct A(u32);

fn main() {
    let mut type_reg = TypeReg::new();
    type_reg.register::<u32>();
    type_reg.register::<u64>();
    type_reg.register::();

    let serialized = "---\n\
        one:   { u32: 1 }\n\
        two:   { u64: 2 }\n\
        three: { 'tagged_deserialize_map::A': 3 }\n\
        ";

    let deserializer = serde_yaml::Deserializer::from_str(serialized);
    let type_map: TypeMap<String> = type_reg.deserialize_map(deserializer).unwrap();

    let data_u32 = type_map.get::<u32, _>("one").copied().unwrap();
    let data_u64 = type_map.get::<u64, _>("two").copied().unwrap();
    let data_a = type_map.get::("three").copied().unwrap();

    println!("{data_u32}, {data_u64}, {data_a:?}");

    // 1, 2, A(3)
}

Untagged Type Registry

Serialization

use serde::{Deserialize, Serialize};
use type_reg::untagged::TypeMap;

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct A(u32);

fn main() {
    let mut type_map = TypeMap::new();
    type_map.insert("one", 1u32);
    type_map.insert("two", 2u64);
    type_map.insert("three", A(3));

    println!("{}", serde_yaml::to_string(&type_map).unwrap());

    // ---
    // two: 2
    // one: 1
    // three: 3
}

Deserialization

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

You might also like...
Online-statistics is crate 🦀 for Blazingly fast, generic and serializable online statistics

Online statistics in Rust 🦀 for Blazingly fast, generic and serializable online statistics. Quickstart Let's compute th

Typed any map for rust

TypedMap TypedMap is a typed HashMap. It allows you to define different value type depending on a Key type. It's useful if you want to store different

Standard Graphics is a command-line tool for printing 2D graphics from any language to any screen.
Standard Graphics is a command-line tool for printing 2D graphics from any language to any screen.

2D graphics in any programming language with just print statements!

sblade or switchblade it's a multitool in one capable of doing simple analysis with any type of data, attempting to speed up ethical hacking activities

sblade or switchblade it's a multitool in one capable of doing simple analysis with any type of data, attempting to speed up ethical hacking activities

A Rust trait to convert numbers of any type and size to their English representation.

num2english This Rust crate provides the NumberToEnglish trait which can be used to convert any* number to its string representation in English. It us

A special rope, designed to work with any data type that is not String

AnyRope AnyRope is an arbitrary data type rope for Rust, designed for similar operations that a rope would do, but targeted at data types that are not

Shiva library: Implementation in Rust of a parser and generator for documents of any type
Shiva library: Implementation in Rust of a parser and generator for documents of any type

Shiva Shiva library: Implementation in Rust of a parser and generator for documents of any type Features Common Document Model (CDM) for all document

This library implements a type macro for a zero-sized type that is Serde deserializable only from one specific value.

Monostate This library implements a type macro for a zero-sized type that is Serde deserializable only from one specific value. [dependencies] monosta

Type erased vector. All elements have the same type.

Type erased vector. All elements have the same type. Designed to be type-erased as far as possible - most of the operations does not know about concre

Build database expression type checker and vectorized runtime executor in type-safe Rust

Typed Type Exercise in Rust Build database expression type checker and vectorized runtime executor in type-safe Rust. This project is highly inspired

Translate C++/Rust type into C type with the same memory layout

clayout, translate C++/Rust type into C type with the same memory layout. Generally, clayout is used together with bpftrace. clayout is developed on d

A crate that allows you to mostly-safely cast one type into another type.

A crate that allows you to mostly-safely cast one type into another type. This is mostly useful for generic functions, e.g. pub fn fooS(s: S) {

ASCII terminal hexagonal map  roguelike written in Rust
ASCII terminal hexagonal map roguelike written in Rust

rhex Contributors welcome! Rhex is looking for contributors. See Contributing page for details. Introduction Simple ASCII terminal hexagonal map rogue

A Bevy plugin for loading the LDtk 2D tile map format.
A Bevy plugin for loading the LDtk 2D tile map format.

bevy_ldtk ( Tileset from "Cavernas" by Adam Saltsman ) A Bevy plugin for loading LDtk tile maps. Usage use bevy::prelude::*; use bevy_ldtk::*; fn mai

Everyday-use client-side map-aware Arch Linux mirror ranking tool

Rate Arch Mirrors This is a tool, which fetches mirrors, skips outdated/syncing Arch Linux mirrors, then uses info about submarine cables and internet

Image density/height map to mesh generator
Image density/height map to mesh generator

Image density/height map to mesh generator About Crates used to generate 2D mesh from images representing density/height map. Algorithm gets source im

Pitch-perfect copy of map generation algorithm from Slay the Spire

sts_map_oracle Pitch-perfect copy of map generation algorithm from Slay the Spire Usage Prints out map layouts in console for given seed: sts_map_orac

A unix-friendly map-reduce parallelization alternative

slb: sharded load balancer Like parallel --pipe --roundrobin but load balancing is performed based on input line hashing. When performing keyed aggreg

A simple Vec-based Map inspired on JavaScript for rust.

A simple alternative to HashMap inspired on JavaScript's Map.

Comments
  • Update crate version to 0.4.0.

    Update crate version to 0.4.0.

    This unblocks the first item in https://github.com/azriel91/peace/issues/28. Ideally we should make the same changes to the tagged type registry, and commonize the Box data types.

    opened by azriel91 1
Owner
Azriel Hoh
Azriel Hoh
A simple Vec-based Map inspired on JavaScript for rust.

A simple alternative to HashMap inspired on JavaScript's Map.

Squioole 2 Oct 3, 2021
B-Tree map for pub/sub services

submap B-tree map for pub/sub services. Create a new subscription map let mut smap: SubMap<Client> = SubMap::new(); where "Client" is a pub/sub client

Altertech 6 Sep 21, 2022
Special FUSE filesystem to map /etc/resolv.conf to different files depending on Linux network namespace

Linux network namespaces allow separate networking environment for a group of processes (sharing uid or from a separate user). DNS settings (/etc/resolv.conf) are however shared between all those environments, which may be inconvenient in some setups.

Vitaly Shukela 2 May 16, 2022
Conditional compilation using boolean expression syntax, rather than any(), all(), not()

Conditional compilation expressions Conditional compilation using boolean expression syntax, rather than any(), all(), not(). [dependencies] efg = "0.

David Tolnay 298 Dec 28, 2022
Some collections to store a fixed number of elements of a specific type.

This repo provide some useful data-structures, such as: Array, HashMap, HashSet, BTreeMap, BTreeSet, etc... That lives on the stack. It's a good choic

null 0 Nov 2, 2022
A redis module that provides a type for inventory deduction in flash sales

A redis module that provides a type for inventory deduction in flash sales

GengTeng 2 Mar 18, 2022
Serializable map of any type.

??️ type_reg Serializable map of any type. This library provides a map that can store any serializable type, and retrieve it as the strong type. Seria

Azriel Hoh 3 Dec 26, 2022
SelfOrgMap 5 Nov 4, 2020
a collection of well-tested, serializable CRDTs for Rust

crdts: family of thoroughly tested hybrid crdt's. A family of CRDT's supporting both State and Op based replication. what is a CRDT? CRDT's are the so

null 1k Jan 8, 2023
Turns running Rust code into a serializable data structure.

WasmBox WasmBox turns running Rust code into a serializable data structure. It does this by compiling it to WebAssembly and running it in a sandbox. T

drifting in space 12 Dec 7, 2022