Created
October 5, 2025 22:15
-
-
Save TethysSvensson/722c781c09981b166257955972152f74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct NegOne; | |
| struct Zero; | |
| struct Shift<N>(PhantomData<N>); | |
| struct Shift1<N>(PhantomData<N>); | |
| type One = Shift1<Zero>; | |
| trait Integer { | |
| const VALUE: i128; | |
| type Shift: Integer; | |
| type Add1: Integer; | |
| type Sub1: Integer; | |
| type Neg: Integer; | |
| } | |
| impl Integer for Zero { | |
| const VALUE: i128 = 0; | |
| type Shift = Zero; | |
| type Add1 = Shift1<Zero>; | |
| type Sub1 = NegOne; | |
| type Neg = Zero; | |
| } | |
| impl Integer for NegOne { | |
| const VALUE: i128 = -1; | |
| type Shift = Shift<NegOne>; | |
| type Add1 = Zero; | |
| type Sub1 = Shift<NegOne>; | |
| type Neg = One; | |
| } | |
| impl<N: NonZero> Integer for Shift<N> { | |
| const VALUE: i128 = N::VALUE << 1; | |
| type Shift = Shift<Shift<N>>; | |
| type Add1 = Shift1<N>; | |
| type Sub1 = Shift1<<N as Integer>::Sub1>; | |
| type Neg = <<N as Integer>::Neg as Integer>::Shift; | |
| } | |
| impl<N: Integer> Integer for Shift1<N> { | |
| const VALUE: i128 = (N::VALUE << 1) | 1; | |
| type Shift = Shift<Shift1<N>>; | |
| type Add1 = <<N as Integer>::Add1 as Integer>::Shift; | |
| type Sub1 = <N as Integer>::Shift; | |
| type Neg = Shift1<<<N as Integer>::Neg as Integer>::Neg>; | |
| } | |
| trait NonZero: Integer {} | |
| impl NonZero for NegOne {} | |
| impl<N: NonZero> NonZero for Shift<N> {} | |
| impl<N: Integer> NonZero for Shift1<N> {} | |
| trait Add<Other: Integer>: Integer { | |
| type Result: Integer; | |
| } | |
| impl<N: Integer> Add<N> for Zero { | |
| type Result = N; | |
| } | |
| impl<N: Integer> Add<N> for NegOne { | |
| type Result = <N as Integer>::Sub1; | |
| } | |
| impl<N: NonZero> Add<Zero> for Shift<N> { | |
| type Result = Shift<N>; | |
| } | |
| impl<N: NonZero> Add<NegOne> for Shift<N> { | |
| type Result = Shift1<<N as Integer>::Sub1>; | |
| } | |
| impl<N: NonZero, M: NonZero> Add<Shift<M>> for Shift<N> | |
| where | |
| N: Add<M>, | |
| { | |
| type Result = <<N as Add<M>>::Result as Integer>::Shift; | |
| } | |
| impl<N: NonZero, M: Integer> Add<Shift1<M>> for Shift<N> | |
| where | |
| N: Add<M>, | |
| { | |
| type Result = Shift1<<N as Add<M>>::Result>; | |
| } | |
| impl<N: Integer> Add<Zero> for Shift1<N> { | |
| type Result = Shift1<N>; | |
| } | |
| impl<N: Integer> Add<NegOne> for Shift1<N> { | |
| type Result = <N as Integer>::Shift; | |
| } | |
| impl<N: Integer, M: NonZero> Add<Shift<M>> for Shift1<N> | |
| where | |
| N: Add<M>, | |
| { | |
| type Result = Shift1<<N as Add<M>>::Result>; | |
| } | |
| impl<N: Integer, M: Integer> Add<Shift1<M>> for Shift1<N> | |
| where | |
| N: Add<M>, | |
| { | |
| type Result = <<<N as Add<M>>::Result as Integer>::Add1 as Integer>::Shift; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment