Comments (6)
I'm down for this change; let's just make sure we're happy with the names and number of different Derives that it's split into.
So right now, I think there's 2 different Derives, unwrap, and try_unwrap
(which I'm thinking should be try_as_
)
impl MyEnum {
// #[derive(Unwrap)]
fn unwrap_variant(self) -> Type {}
fn unwrap_variant_ref(&self) -> &Type {} // For Copy types, this is probably not useful, but not sure what to do with that.
fn unwrap_variant_mut(&mut self) -> &mut Type {}
// It's probably reasonable to choose a "default" for unwrapping that gets to be just `unwrap`, but that could
// be a later feature
// #[derive(TryAs)]
fn try_as_variant(self) -> Option<Type> {}
fn try_as_variant_ref(&self) ->Option<&Type> {}
fn try_as_variant_mut(&mut self) -> Option<&mut Type> {}
}
from strum.
I also just had another idea that I'd like to be able to derive: a version of the Result::ok
method for each variant to get an option.
impl Foo {
pub fn single(&self) -> Option<bool> {
match self {
Self::Single(b) => Some(b),
_ => None,
}
}
}
This would be useful in map methods, since you could pull all the variants out like this:
let vec: Vec<Foo> = vec![...];
let singles: Vec<bool> = vec.iter().filter_map(Foo::single).collect();
Edit: This might be better as a separate feature from unwraps
from strum.
I wrote the rest of the comment before looking at the is_*
methods. Given that those were ultimately added to Strum, I think these will be as well. If you start a fork/PR, I'd love to be added as a collaborator!
This is definitely possible, but I'm not sure how useful it would be. Consider the following pattern:
// vanilla rust
let Foo::Double(integer, string) = foo else { panic!() };
// using unwraps
let (integer, string) = foo.unwrap_double();
// vanilla rust
let Foo::Named{a, b} = foo else { panic!() };
// no way to do this with these unwraps
I assume you meant that adding unwrap to Foo::Simple
wouldn't make sense?
from strum.
I assume you meant that adding unwrap to Foo::Simple wouldn't make sense?
Good catch! I fixed my original comment.
from strum.
@Peternator7 also:
unwrap_variant_or
, unwrap_variant_or_else
, unwrap_variant_unchecked
,
unwrap_variant_ref_or
, unwrap_variant_ref_or_else
, unwrap_variant_ref_unchecked
.
Probably doesn't make any sense to do the same on mut
versions.
Don't think we need all these for try as, since you can just use the Option methods on there.
from strum.
I know that I was the one to propose the unwrap methods in the first place, but after seeing the try_as methods, I'm not sure that they're still needed.
You can basically achieve the same result as foo.unwrap_variant()
by doing foo.try_as_variant().unwrap()
.
And by using Option
's unwrap methods, we get the other goodies like unwrap_or
, unwrap_or_else
, unwrap_unchecked
, expect
, without having to pay anything in compile time.
Thoughts?
cc @Peternator7 @PokeJofeJr4th
from strum.
Related Issues (20)
- EnumIter does not work for enums named 'Option'
- [Feature] Macro to Implement PartialEq<str> HOT 1
- strum::Display doesn't work with non-unit non-string variants
- `EnumString` should derive `From<&str>` instead of `TryFrom<&str>` when `#[strum(default)]` is present HOT 1
- to_string and serialize when used together causes unreachable code HOT 2
- `get_bool` and `get_int` in `EnumProperty` are undocumented and always return None
- AsRef<str> and default variants
- EnumDiscriminants: can't infer type HOT 3
- Getting the discriminant from an enum variant HOT 3
- Publish new release. HOT 4
- AsRefStr documentation incorrectly states 'static ref is returned HOT 2
- Publish Git tags HOT 2
- TryFrom<Repr> for enums using FromRepr? HOT 2
- Implement `Iterator` for EnumTable HOT 6
- Customize error message for FromStr
- Documentation for `VariantArray` and `VariantNames` say their `VARIANTS` constants are arrays, but they're actually slices HOT 2
- Support for const_into_str to Enable Static String Conversion in Const Context
- Accessing the EnumTable type through the enum HOT 1
- Error updating to 0.26 - trait bound `std::iter::FusedIterator` is not satisfied HOT 1
- Itertools 0.13.0 and strum derive macro EnumIter break if both are used at the same time HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strum.