ratmice / enum_extra Goto Github PK
View Code? Open in Web Editor NEWDerive macros and Traits for working with enum reprs.
License: Apache License 2.0
Derive macros and Traits for working with enum reprs.
License: Apache License 2.0
currently not all bounds required by MaskIterator are enforced, some are just documented.
A MaskIterator which iterates over unique single bit non-zero enum variants.
Unique single-bit also entails unsigned, or bounded > 0, as the sign lacks uniqueness.
if I recall the implementation correctly it does not need to be contiguous.
It is probably worth enforcing trait bounds for these eventually.
I haven't found the place where default type for enum repr's are given, and what guarantees the compiler gives,
Currently NonZeroRepr
requires #[repr(..)]
to be set explicitly, and doesn't default to any type.
It appears the compilers though do have a default type, which does not change.
e.g.
enum Foo {
Bar = std::usize::MAX,
}
error[E0308]: mismatched types
--> src/main.rs:2:11
|
2 | Bar = std::usize::MAX,
| ^^^^^^^^^^^^^^^ expected `isize`, found `usize`
and similar for anything attempting to
error[E0308]: mismatched types
--> src/main.rs:2:11
|
2 | Bar = -1_i8,
| ^^^^^ expected `isize`, found `i8`
need to find if/where this is guaranteed, before I loosen the restriction...
Ideally it'd be nice to be able to use this with Repr(C) but really idea (with the caveat that I haven't really tried to see if there is actually some sane mechansim) for deciding what type what type Repr(C) would pick.
This whole lib uses a bunch of really strange generic/trait magic (like traits with equality constraints over associated types that are actually Self
),
It is worth thinking about how/if it can be simplified: Is it possible to ditch the associated types and merely use Self
?
I don't offhand believe so, because we only get equality constraints for traits and they aren't supported in a where clause.
But certainly there seems like a lot of complexity here and it is likely that some simplifications could be made.
For instance, it'd probably be better to rename OpaqueMetadata::Repr
to something else so as to avoid inducing ambiguity with EnumMetadata::Repr
.
Eventually I want to add traits like SingleBitUnique
, ExistsUniqueBit
where ExistsUniqueBit
would accept:
Foo = 0x5,
Bar = 0x3,
because even though Foo
and Bar
overlap at bit 1, they are unique in bits 2 and 3.
where SingleUniqueBit
works with enums like:
Foo = 1 << 0,
Bar = 1 << 1,
These are okay, but think if there are better names.
Also its worth thinking whether it should bundle traits together,
for instance MaskIterator wants: NonZeroRepr + SingleBitUnique
Should we define a trait which combines these 2 or leave it to MaskIterator to combine the bounds...
Not sure if it is worthwhile, but we could have a type ValidRepr<T>
which does not implement arithmetic operators.
It would be constructable from an enum variant and back into an enum variant using to_valid_repr
and a variant from_valid_repr
would not need to return an option, potentially having an unsafe unchecked
constructor.
I'm not certain it is worthwhile though, it is probably worth having a use case in place before adding it.
Potential generic use case: all arithmetic is done in a private function (not publicly, but converting from ValidRepr<T>
to the T::Repr
), and all cases that arithmetic on T::Repr
will produce a T
.
by all means chime in if you have a concrete use case for such a thing.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.