Comments (7)
Makes sense. Thanks!
from getargs.
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 str
s (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.
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.
Actually, I realized that you would need an arguments array that lives for
'static
in order to get anOptions<'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.
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.
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.
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)
- Create `Options` from iterator HOT 6
- Publishing 0.5.0 HOT 4
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 getargs.