Crate dt

Source
Expand description

Collection of primal data types.

§Module :: data_type

experimental rust-status docs.rs Open in Gitpod discord

Collection of primal data types.

§Sample :: 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 >;
  }
}

§Sample :: make - variadic constructor

Implement traits Make0, Make1 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 Make0 for Struct1
  {
    fn make_0() -> Self
    {
      Self { a : 0, b : 0 }
    }
  }

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

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

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

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

  let got : Struct1 = make!( 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 sample/rust/type_constructor_multiple_sample
cargo run

Modules§

dependency
Dependencies.
dt
Collection of primal data types.
exposed
Exposed namespace of the module.
orphan
Shared with parent namespace of the module
prelude
Prelude to use essentials: use my_module::prelude::*.
protected
Protected namespace of the module.

Macros§

_if_make
Generate code only if feature::make is enabled.
_many
Type constructor of many.
_pair
Pair type constructor.
_single
Type constructor of single.
_vec
Alias of Vec for internal usage.
make
Variadic constructor.
types
Type constructor to define tuple wrapping a given type.

Structs§

BTreeMap
An ordered map based on a B-Tree.
BTreeSet
An ordered set based on a B-Tree.
BinaryHeap
A priority queue implemented with a binary heap.
DynArray
A contiguous growable array type, written as Vec<T>, short for ‘vector’.
EnumerableIteratorConsumable
Iterator for enumerable.
EnumerableIteratorNonConsumable
Iterator for enumerable.
HashMap
A hash map implemented with quadratic probing and SIMD lookup.
HashSet
A hash set implemented as a HashMap where the value is ().
HomoPair
Type constructor to wrap pair of the same type.
Interval
Alternative implementation of interval.
LinkedList
A doubly-linked list with owned nodes.
Many
Type constructor to wrap a vector.
Map
A hash map implemented with quadratic probing and SIMD lookup.
Pair
Type constructor to wrap two types into a tuple.
Set
A hash set implemented as a HashMap where the value is ().
Single
Type constructor to wrap a another type into a tuple.
Vec
A contiguous growable array type, written as Vec<T>, short for ‘vector’.
VecDeque
A double-ended queue implemented with a growable ring buffer.
_Vec
A contiguous growable array type, written as Vec<T>, short for ‘vector’.

Enums§

Either
The enum Either with variants Left and Right is a general purpose sum type with two cases.

Traits§

AsArray
Reinterpret as array.
AsSlice
Reinterpret as slice.
AsTuple
Reinterpret as tuple.
CloneAsArray
Clone as array.
CloneAsTuple
Clone as tuple.
Enumerable
Has length and indexed access.
IntervalAdapter
Interval adapter. Interface to interval-like structures.
Make0
Constructor without arguments.
Make1
Constructor with single argument.
Make2
Constructor with two arguments.
Make3
Constructor with three arguments.
VectorizedFrom
Implementation of trait From to vectorize into/from.
VectorizedInto
Implementation of trait Into to vectorize into/from.