#name #serde #type-name #type

serde_type_name

lookup name of type for any struct or enum that derives serde Serialize

2 unstable releases

Uses old Rust 2015

0.2.0 Feb 24, 2019
0.1.0 Aug 24, 2018

#2497 in Rust patterns

Download history 76/week @ 2024-07-21 27/week @ 2024-07-28 36/week @ 2024-08-04 83/week @ 2024-08-11 21/week @ 2024-08-18 55/week @ 2024-08-25 32/week @ 2024-09-01 279/week @ 2024-09-08 247/week @ 2024-09-15 113/week @ 2024-09-22 160/week @ 2024-09-29 385/week @ 2024-10-06 201/week @ 2024-10-13 284/week @ 2024-10-20 132/week @ 2024-10-27 432/week @ 2024-11-03

1,120 downloads per month
Used in 4 crates (3 directly)

MIT/Apache

12KB
241 lines

Build Status Current Crates.io Version

Serde Type Name

Serde Type Name - lookup name of type for any struct or enum that derives serde Serialize. This is completely based on

https://stackoverflow.com/questions/46612327/using-serde-to-get-a-types-name-as-string-when-the-type-is-only-serialize

Usage

extern crate serde_type_name;
extern crate serde;
#[macro_use]
extern crate serde_derive;

use serde::Serialize;
use serde_type_name::type_name;

#[derive(Serialize)]
enum SimpleEnum {
    A(i32),
    B,
    C { code: i32 },
}

#[derive(Serialize)]
struct SimpleStruct {
    pub a: i32,
}

#[derive(Serialize)]
struct GenericStruct<T: Serialize> {
    pub inner: T,
}

fn main() {
    assert_eq!("SimpleEnum", type_name(&SimpleEnum::A(10)).unwrap());
    assert_eq!("SimpleEnum", type_name(&SimpleEnum::B).unwrap());
    assert_eq!("SimpleEnum", type_name(&SimpleEnum::C { code: 32 }).unwrap());
    
    assert_eq!("SimpleStruct", type_name(&SimpleStruct { a: 20 }).unwrap());
    
    let gs = GenericStruct { inner: 32 };
    assert_eq!("GenericStruct", type_name(&gs).unwrap());
    
    let gs_enum = GenericStruct {
        inner: SimpleEnum::B,
    };
    assert_eq!("GenericStruct", type_name(&gs_enum).unwrap());
    assert_eq!("SimpleEnum", type_name(&gs_enum.inner).unwrap());
}

Dependencies

~100–335KB