Crates.io | number-types |
lib.rs | number-types |
version | 0.1.0 |
source | src |
created_at | 2023-11-14 01:20:47.692083 |
updated_at | 2023-11-14 01:20:47.692083 |
description | A typenum clone I made for fun with some positive changes but much fewer features |
homepage | |
repository | |
max_upload_size | |
id | 1034227 |
size | 11,636 |
A primitive, albeit with slight improvements, recreation of the typenum crate I made in order to get more familiar with the typesystem.
It uses its own traits and only uses types, never instances. Not a single instance is ever created.
That's basically it, everything else is probably worse. This is just for fun.
There's a few macros for easier expression-crafting. They always run the operations
in the same order: type_expr!(<a> * <b> + <c> - <d>)
will do (a * (b + (c - d)))
.
Weird, but not my choice. I just didn't want to spend THAT long on a macro.
There's types called Z0..Z10 and Z100, and negative counterparts for each (ZN...).
To craft operations yourself, use <Op<Lhs, Rhs> as OpKindOp>::Output
(OpKind should be
Add, Mul, or Sub). To negate a number, use <N as Neg>::Output
.
You can assert type equality using assert_type_eq!(a, b).
All numbers are simply N wrapping layers of Successor
around Zero. Negative numbers
also get a Negative
wrapper at the top layer.
Addition is done essentially like this:
Subtraction is done like this:
This includes some more cases to handle negative numbers (since they can turn the subtraction into what's basically an addition), but I won't explain those here.
Multiplication is repeated addition.
Did you know you can pattern match types?
impl<A: Constr, B: Constr, O> TraitA<B> for SomeWrapper(A)
where
SomeOtherOrSameWrapper(A): TraitB<B, Output = O>
{
type Output = O;
}
:3