20次重大发布

0.22.0 2024年8月16日
0.21.0 2024年7月13日
0.20.0 2024年6月29日
0.13.0 2024年3月26日
0.2.0 2023年10月28日

#880开发工具

Download history 7/week @ 2024-04-26 30/week @ 2024-05-03 3059/week @ 2024-05-10 1547/week @ 2024-05-17 1350/week @ 2024-05-24 3117/week @ 2024-05-31 818/week @ 2024-06-07 358/week @ 2024-06-14 218/week @ 2024-06-21 1067/week @ 2024-06-28 255/week @ 2024-07-05 1357/week @ 2024-07-12 605/week @ 2024-07-19 664/week @ 2024-07-26 230/week @ 2024-08-02 829/week @ 2024-08-09

2,689 每月下载量
56 个Crate中使用(通过 derive_tools

MIT 许可证

34KB
577

模块 :: variadic_from

experimental rust-status docs.rs Open in Gitpod discord

可变参数From模块旨在提供一种为具有可变数量字段的结构体实现类似From特质的方法,允许它们从不同长度的元组或单个参数构建。此功能特别适用于创建灵活的构造函数,使结构体能够以不同的方法实例化。通过自动化特质的实现,Crate减少了样板代码并提高了代码的可读性和可维护性。

目前它支持最多3个参数。如果你的结构体有超过3个字段,derive将不会生成任何内容。它还支持元组转换,允许结构体利用FromInto特质从元组实例化结构体,实现无缝转换。

基本用例。

此示例演示了使用variadic_from宏为结构体实现灵活构造函数,允许从不同数量的参数或元组实例化它。它还展示了如何为结构体推导出常见的特质,如DebugPartialEqDefaultVariadicFrom

#[ cfg( not( all(feature = "enabled", feature = "type_variadic_from", feature = "derive_variadic_from" ) ) ) ]
fn main(){}
#[ cfg( all( feature = "enabled", feature = "type_variadic_from", feature = "derive_variadic_from" ) )]
fn main()
{
  use variadic_from::exposed::*;

  // Define a struct `MyStruct` with fields `a` and `b`.
  // The struct derives common traits like `Debug`, `PartialEq`, `Default`, and `VariadicFrom`.
  #[ derive( Debug, PartialEq, Default, VariadicFrom ) ]
  // Use `#[ debug ]` to expand and debug generate code.
  // #[ debug ]
  struct MyStruct
  {
    a : i32,
    b : i32,
  }

  // Implement the `From1` trait for `MyStruct`, which allows constructing a `MyStruct` instance
  // from a single `i32` value by assigning it to both `a` and `b` fields.

  impl From1< i32 > for MyStruct
  {
    fn from1( a : i32 ) -> Self { Self { a, b : a } }
  }

  let got : MyStruct = from!();
  let exp = MyStruct { a : 0, b : 0 };
  assert_eq!( got, exp );

  let got : MyStruct = from!( 13 );
  let exp = MyStruct { a : 13, b : 13 };
  assert_eq!( got, exp );

  let got : MyStruct = from!( 13, 14 );
  let exp = MyStruct { a : 13, b : 14 };
  assert_eq!( got, exp );

  dbg!( exp );
  //> MyStruct {
  //>   a : 13,
  //>   b : 14,
  //> }

}
上面的代码将扩展为以下内容
#[ cfg( not( all(feature = "enabled", feature = "type_variadic_from" ) ) ) ]
fn main(){}
#[ cfg( all( feature = "enabled", feature = "type_variadic_from" ) )]
fn main()
{
  use variadic_from::exposed::*;

  // Define a struct `MyStruct` with fields `a` and `b`.
  // The struct derives common traits like `Debug`, `PartialEq`, `Default`
  // `VariadicFrom` defined manually.
  #[ derive( Debug, PartialEq, Default ) ]
  struct MyStruct
  {
    a : i32,
    b : i32,
  }

  // Implement the `From1` trait for `MyStruct`, which allows constructing a `MyStruct` instance
  // from a single `i32` value by assigning it to both `a` and `b` fields.
  impl From1< i32 > for MyStruct
  {
    fn from1( a : i32 ) -> Self { Self { a, b : a } }
  }

  // == begin of generated

  impl From2< i32, i32 > for MyStruct
  {
    fn from2( a : i32, b : i32 ) -> Self { Self{ a : a, b : b } }
  }

  impl From< ( i32, i32 ) > for MyStruct
  {
    #[ inline( always ) ]
    fn from( ( a, b ) : ( i32, i32 ) ) -> Self
    {
      Self::from2( a, b )
    }
  }

  // == end of generated

  let got : MyStruct = from!();
  let exp = MyStruct { a : 0, b : 0 };
  assert_eq!( got, exp );

  let got : MyStruct = from!( 13 );
  let exp = MyStruct { a : 13, b : 13 };
  assert_eq!( got, exp );

  let got : MyStruct = from!( 13, 14 );
  let exp = MyStruct { a : 13, b : 14 };
  assert_eq!( got, exp );

  dbg!( exp );
  //> MyStruct {
  //>   a : 13,
  //>   b : 14,
  //> }

}

尝试运行 cargo run --example variadic_from_trivial
查看代码.

将添加到您的项目中

cargo add variadic_from

从仓库中尝试

git clone https://github.com/Wandalen/wTools
cd wTools
cargo run --example variadic_from_trivial

依赖项

~2MB
~41K SLoC