#type #typed #protocols #dismantle

dismantler

获取结构体的类型

1 个不稳定发布

0.0.1 2023年1月22日

#147#typed

MIT/Apache

22KB
375

拆解器

github crates.io

Dismantler 是一个用于将 structsfns 拆解成其内部 type 组件的过程宏,这些组件随后将伴随文档和示例。 Dismantler 结构将被封装到一个模块中,并重新命名为名称(默认 core),这也适用于 staticgeneric 字段。

项目仍在开发中

示例

[dependencies]
dismantler = "0.0.1"
use dismantler::dismantle;

#[dismantle]
struct Containter<T> {
    current: u8,
    buffer: Vec<u8>,
    another: T,
}
#[dismantle]
struct Area(i32);
  • 允许您按以下方式访问结构体类型
let current: Container::current = 10;
let buffer: Container::buffer = vec![current];
let another: <Container::core<u8> as Container::protocol>::another = 20;
let container: Container::core<u8> = 
    Container::core {
        current,
        buffer,
        another
    };
  • 也可以按以下方式使用它
trait Trait: Container::protocol {
    fn retrieve(&self) -> Container::protocol::buffer;
    fn extend(&mut self, val: Container::protocol::another); 
}

更多

use dismantler::dismantle;

#[dismantle]
struct Container<C: Clone, T = i64> {
    a: i32,
    b: Vec<i32>,
    c: Vec<T>,
    d: C,
    e: T
}

#[dismantle]
struct Tuple(i32, i32);

#[dismantle]
struct Tuple2<T>(i32, T);

fn main() {
    let a: Container::fields::a = 10;
    let b: Container::b = vec![a];
    let c: Container::c<i64> = vec![10];
    let c: <Container::core<i64> as Container::protocol>::c = c;
    let d: <Container::core<i64> as Container::protocol>::d = 10;
    let container: Container::core<i64> = Container::core { a, b, c, d, e: 10 };

    assert!(container.a == a);
}

当前支持

  • 结构体staticgeneric
  • 函数staticgeneric

反汇编器

#[dismantle]
struct #name {
    #(#ident: #ty)*
}

// Turns into

#[allow(non_snake_case)]
// Docs (/w examples) describing the original `item` and also what `types` are available to use.
#[doc = #docs] 
pub mod #name {
    #![allow(non_camel_case_types)]
    
    // The static fields of the `item` as type aliases.
    #(#ty_decls)* // Access through `#name::#field`
    
    // A trait where all `ìtem` fields are associated types
    #struct_generic // Access through `#name::gen`
    
    // Docs (/w examples) describing the original `item`.
    #[doc = #docs]
    // The original `ìtem`.
    #struct_original // Access through `#name::core`
}

未来计划

重命名反汇编器

use dismantler::dismantle;

#[dismantle = "MContainer"]
struct Containter<T> {
    current: u8,
    buffer: Vec<u8>,
    another: T,
}

fn main() {
    let x: Container<i32> = {
        current: 10,
        buffer: Vec::default(),
        another: 20,
    }
    
    let y: MContainer::core<i32> = x;
}

依赖项

~10MB
~209K SLoC