Code Monkey home page Code Monkey logo

Comments (6)

Peternator7 avatar Peternator7 commented on May 24, 2024 2

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.

PokeJofeJr4th avatar PokeJofeJr4th commented on May 24, 2024 1

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.

PokeJofeJr4th avatar PokeJofeJr4th commented on May 24, 2024

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.

MendyBerger avatar MendyBerger commented on May 24, 2024

I assume you meant that adding unwrap to Foo::Simple wouldn't make sense?

Good catch! I fixed my original comment.

from strum.

MendyBerger avatar MendyBerger commented on May 24, 2024

@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.

MendyBerger avatar MendyBerger commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.