Open jymchng opened 9 months ago
I would guess that you could implement MinimallyRepresentableUInt
for all U*
generically.
I don't really have time to do this right now, but if you look at the implementations of some of the more complex functionality of typenum (such as division or gcd), it may give you some ideas.
Basically, Rust's type system is turing complete, so you can do pretty much anything. But the only tools are essentially recursion and branching, so code gets messy fast.
I would guess that you could implement
MinimallyRepresentableUInt
for allU*
generically.I don't really have time to do this right now, but if you look at the implementations of some of the more complex functionality of typenum (such as division or gcd), it may give you some ideas.
Basically, Rust's type system is turing complete, so you can do pretty much anything. But the only tools are essentially recursion and branching, so code gets messy fast.
@paholg
Thank you for your response. It's cool now, I've gotten help from another genius (like yourself :) on the Rust forum.
https://users.rust-lang.org/t/making-a-type-level-type-function-with-typenum-crate/107008/4?
Context:
I am writing
sosecrets-rs
.Let's imagine that I have the following struct:
The intent of
RunTimeSecret
is to 'panic' or return an error only ifexposure_count
is greater thanMEC::USIZE
.Now, I can always make the field
exposure_count
to be of typeusize
but that will be a waste because ifMEC
=U69
, thenu8
would be all that's needed to representexposure_count
since it is never larger than69
.Summary:
I am trying to use
typenum
type-level integers to conditionally type fields of structs.Attempt:
I wrote the following:
Sadly, this is definitely not going to scale for all U<*>, i.e.
U69
,U64905405
, etc.Of course,
this doesn't work because const generics expressions are not on stable Rust.
Just curious if there is a way to implement such a trait to all
U<*>
even if it involves macros and what not.Playground