3 releases

0.0.3 Sep 28, 2021
0.0.2 Sep 28, 2021
0.0.1 Sep 28, 2021

#4 in #go-to-rust

Download history 5/week @ 2024-01-08 9/week @ 2024-01-29 20/week @ 2024-02-05 16/week @ 2024-02-12 29/week @ 2024-02-19 43/week @ 2024-02-26 23/week @ 2024-03-04 32/week @ 2024-03-11 82/week @ 2024-03-18

181 downloads per month
Used in 8 crates (2 directly)

MIT license

6KB

gostd_derive

crates.io Released API docs GPL3 licensed Downloads of Crates.io Lines of code Build Languages

proc_macro_derive library for gostd.

Fmt

用宏模拟实现Go中的Stringer接口。 在Go中printf函数,自动打印自定义实现的String方法返回内容。

  • 使用方法

#[derive(Fmt)]

example:

use gostd_derive::Fmt;

#[derive(Fmt)]
struct Foo{
  name:String,
}

// 必须为附加Fmt继承宏的Struct 或者 Emun 实现String方法才能正常运行
impl Foo {

    fn String()->String{
        "test".to_string()
    }
}
  • 功能逻辑

Fmt功能就是继承Display 并调用String()方法,在println!()实现自定义打印格式。

功能的rust表示如下。

impl fmt::Display for Foo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.String())
    }
} 
  • 如何调试

本库只使用官方的proc_macro没有办法调试。 唯一方法,只有运行 cargo check 检查,不报错就没问题。

Dependencies

~1.5MB
~33K SLoC