#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

#1444 in Rust patterns

Download history 32/week @ 2024-03-04 23/week @ 2024-03-11 23/week @ 2024-03-18 48/week @ 2024-03-25 88/week @ 2024-04-01 44/week @ 2024-04-08 54/week @ 2024-04-15 29/week @ 2024-04-22 31/week @ 2024-04-29 56/week @ 2024-05-06 36/week @ 2024-05-13 38/week @ 2024-05-20 61/week @ 2024-05-27 50/week @ 2024-06-03 83/week @ 2024-06-10 31/week @ 2024-06-17

227 downloads per month
Used in 2 crates

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

~110–355KB