GithubHelp home page GithubHelp logo

Comments (7)

j-tai avatar j-tai commented on June 1, 2024 1

Makes sense. Thanks!

from getargs.

j-tai avatar j-tai commented on June 1, 2024

The strings being used with Options are 'static, so I would've expected this to work. Is there any reason why it shouldn't, or is this a mistake?

They are not 'static. Long options contain a &'a str which is a reference to the arguments that you provided. Remember, getargs doesn't know which options are valid and which aren't, so it gives you whatever options it finds in the input.

In this case I would recommend making your Error enum have a lifetime, like this:

enum Error<'a> {
    DoesNotRequireValueLong(&'a str),
    ...
}

That way your error has the same lifetime as the arguments array that you passed into the parser.

If you actually pass in &'static strs (or similar) into the Options constructor, then you should get an Options<'static, S>, and you should be able to extract a &'static str from that.

from getargs.

j-tai avatar j-tai commented on June 1, 2024

Actually, I realized that you would need an arguments array that lives for 'static in order to get an Options<'static, S>, even if the strings in the array are 'static. This is a bit tricky...

The issue is that Options accepts any impl AsRef<str> to parse. So it only has one lifetime to work with -- the lifetime of the array, not the lifetimes of the individual strings. It needs to call as_ref() in order to read the string and return substrings. These strings have the same lifetime as the array. Basically we need to have a second lifetime for the individual strings, which we don't have right now.

from getargs.

LoganDark avatar LoganDark commented on June 1, 2024

Actually, I realized that you would need an arguments array that lives for 'static in order to get an Options<'static, S>, even if the strings in the array are 'static. This is a bit tricky...

My options array does live for 'static. I leak it at the start of my program.

The issue is that Options accepts any impl AsRef<str> to parse. So it only has one lifetime to work with -- the lifetime of the array, not the lifetimes of the individual strings.

In this case the lifetime of the array is also 'static.

Basically we need to have a second lifetime for the individual strings, which we don't have right now.

I had this "showerthought" when sleeping earlier. If you're OK with a breaking pull request to fix this, I would be glad to open one. I'm not sure to what extent you're willing to accept breaking changes.

from getargs.

j-tai avatar j-tai commented on June 1, 2024

My options array does live for 'static. I leak it at the start of my program.

If that's the case, then I'm not really sure where the 'a lifetime comes from.

If you're OK with a breaking pull request to fix this, I would be glad to open one.

Sounds good to me. I think this could be implemented together with #1, so we could change Options to something like:

struct Options<'a, I>
where
    I: Iterator<Item = &'a str>

then use a peekable iterator to parse the options.

from getargs.

LoganDark avatar LoganDark commented on June 1, 2024

If that's the case, then I'm not really sure where the 'a lifetime comes from.

My theory is that it comes from the &self borrow in next?

I think this could be implemented together with #1, so we could change Options to something like:

struct Options<'a, I>
where
    I: Iterator<Item = &'a str>

then use a peekable iterator to parse the options.

This looks like it would work great, then people could use any AsRef (via map) but also anything else (like argv).

from getargs.

LoganDark avatar LoganDark commented on June 1, 2024

I found the issue. Some of your functions do not annotate the 'a lifetime parameter of getargs::Result. So, Rust attempts to "un-elide" it as being the lifetime of &self.

This will be fixed in my PR.

from getargs.

Related Issues (3)

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.