#name #serde #type-name #type

serde_type_name

查找任何派生自serde Serialize的struct或enum的类型名称

2个不稳定版本

使用旧的Rust 2015

0.2.0 2019年2月24日
0.1.0 2018年8月24日

Rust模式中排名1444

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
2 crate中使用

MIT/Apache

12KB
241

Build Status Current Crates.io Version

Serde Type Name

Serde Type Name - 为派生自serde Serialize的任何struct或enum查找类型名称。这完全基于

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

使用方法

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());
}

依赖项

~110–355KB