15 releases (5 breaking)

0.6.0 Mar 16, 2024
0.5.0 Mar 16, 2024
0.4.0 Mar 16, 2024
0.3.0 Mar 16, 2024
0.1.5 May 31, 2022

#1726 in Development tools

Download history 35/week @ 2023-12-30 123/week @ 2024-01-06 185/week @ 2024-01-13 81/week @ 2024-01-20 55/week @ 2024-01-27 64/week @ 2024-02-03 121/week @ 2024-02-10 133/week @ 2024-02-17 198/week @ 2024-02-24 178/week @ 2024-03-02 159/week @ 2024-03-09 666/week @ 2024-03-16 217/week @ 2024-03-23 209/week @ 2024-03-30 124/week @ 2024-04-06 165/week @ 2024-04-13

742 downloads per month
Used in 80 crates (3 directly)

MIT license

34KB
632 lines

Module :: data_type

experimental rust-status docs.rs Open in Gitpod discord

Collection of primal data types.

Basic use-case :: type constructors

In Rust, you often need to wrap a given type into a new one. The role of the orphan rules in particular is basically to prevent you from implementing external traits for external types. To overcome the restriction developer usually wrap the external type into a tuple introducing a new type. Type constructor does exactly that and auto-implement traits From, Into, Deref and few more for the constructed type.

Macro types is responsible for generating code for Single, Pair, Homopair, Many. Each type constructor has its own keyword for that, but Pair and Homopair use the same keyword difference in a number of constituent types. It is possible to define all types at once:

#[ cfg( feature = "type_constructor" ) ]
{
  use data_type::prelude::*;

  types!
  {
    pub single MySingle : f32;
    pub single SingleWithParametrized : std::sync::Arc< T : Copy >;
    pub single SingleWithParameter : < T >;

    pub pair MyPair : f32;
    pub pair PairWithParametrized : std::sync::Arc< T1 : Copy >, std::sync::Arc< T2 : Copy >;
    pub pair PairWithParameter : < T1, T2 >;

    pub pair MyHomoPair : f32;
    pub pair HomoPairWithParametrized : std::sync::Arc< T : Copy >;
    pub pair HomoPairWithParameter : < T >;

    pub many MyMany : f32;
    pub many ManyWithParametrized : std::sync::Arc< T : Copy >;
    pub many ManyWithParameter : < T >;
  }
}

Basic use-case :: make - variadic constructor

Implement traits [From_0], [From_1] up to MakeN to provide the interface to construct your structure with a different set of arguments. In this example structure, Struct1 could be constructed either without arguments, with a single argument, or with two arguments.

  • Constructor without arguments fills fields with zero.
  • Constructor with a single argument sets both fields to the value of the argument.
  • Constructor with 2 arguments set individual values of each field.
#[ cfg( feature = "make" ) ]
{
  use type_constructor::prelude::*;

  #[ derive( Debug, PartialEq ) ]
  struct Struct1
  {
    a : i32,
    b : i32,
  }

  impl From_0 for Struct1
  {
    fn from_0() -> Self
    {
      Self { a : 0, b : 0 }
    }
  }

  impl From_1< i32 > for Struct1
  {
    fn from_1( val : i32 ) -> Self
    {
      Self { a : val, b : val }
    }
  }

  impl From_2< i32, i32 > for Struct1
  {
    fn from_2( val1 : i32, val2 : i32 ) -> Self
    {
      Self { a : val1, b : val2 }
    }
  }

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

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

  let got : Struct1 = from!( 1, 3 );
  let exp = Struct1{ a : 1, b : 3 };
  assert_eq!( got, exp );
}

To add to your project

cargo add data_type

Try out from the repository

git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/type_constructor_multiple
cargo run

Dependencies