Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requiring version string have static lifetime makes for awkward code #28

Closed
jhelwig opened this issue Mar 23, 2015 · 4 comments
Closed

Comments

@jhelwig
Copy link

jhelwig commented Mar 23, 2015

In trying to get the version string to automatically populate based on the version I specify in my Cargo.toml using something like:

...
    let version = format!("{}.{}.{}{}",
                          env!("CARGO_PKG_VERSION_MAJOR"),
                          env!("CARGO_PKG_VERSION_MINOR"),
                          env!("CARGO_PKG_VERSION_PATCH"),
                          option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""));

    let matches = App::new("signing-participants")
        .version(&version[..])
...

However, this fails with:

% cargo build
   Compiling signing-participants v0.0.1 (file:///Users/jacobhelwig/btsync/projects/signing-participants)
src/main.rs:27:19: 27:26 error: `version` does not live long enough
src/main.rs:27         .version(&version[..])
                                 ^~~~~~~
note: reference must be valid for the static lifetime...
src/main.rs:24:78: 93:2 note: ...but borrowed value is only valid for the block suffix following statement 1 at 24:77
src/main.rs:24                           option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""));
src/main.rs:25
src/main.rs:26     let matches = App::new("signing-participants")
src/main.rs:27         .version(&version[..])
src/main.rs:28         .author("Jacob Helwig <[email protected]")
src/main.rs:29         .about("Generate a list for key-signing parties")
               ...
error: aborting due to previous error
Could not compile `signing-participants`.

To learn more, run the command again with --verbose.

I can get it to work if I don't care about the prerelease marker, and switch to concat! (haven't been able to get concat! to play nicely with option_env!):

    let version = concat!(env!("CARGO_PKG_VERSION_MAJOR"), ".",
                          env!("CARGO_PKG_VERSION_MINOR"), ".",
                          env!("CARGO_PKG_VERSION_PATCH"));

    let matches = App::new("signing-participants")
        .version(&version)
% cargo run -- --version
   Compiling signing-participants v0.0.1 (file:///Users/jacobhelwig/btsync/projects/signing-participants)
     Running `target/debug/signing-participants --version`
signing-participants 0.0.1

Sticking with format! so I can use option_env! is really not nice (as it involves unsafe code provided by dougxxx in the #rust IRC channel):

#![feature(collections)]
use std::mem::forget;
use std::mem::transmute;

unsafe fn as_static(v:&str) -> &'static str {
  let tmp = String::from_str(v);
  let boxed = Box::new(tmp);
  let rtn: *const str = &**boxed as *const str; 
  forget(boxed);
  return transmute(rtn);
}

fn awkward_function(v:&'static str) {
  println!("{}", v);
}

fn main() {
  let foo = String::from_str("Hello World");
  unsafe { awkward_function(as_static(&*foo)); }
}

Is there a way the elements of the App struct could be changed to not require static lifetimes on its elements? I haven't looked at what the implications of this would be with the rest of the code, but allowing the format! trick I initially tried for defining the version seems like reasonable usage of the library.

@jhelwig jhelwig changed the title Requiring version string be static makes for awkward code Requiring version string have static lifetime makes for awkward code Mar 23, 2015
@kbknapp
Copy link
Member

kbknapp commented Mar 23, 2015

I'll take a look at this tonight and report back. Thanks for the detailed write up!

@jhelwig
Copy link
Author

jhelwig commented Mar 23, 2015

Thanks for taking a look! This library has been working really well for me so far. :)

kbknapp added a commit that referenced this issue Mar 24, 2015
@kbknapp
Copy link
Member

kbknapp commented Mar 24, 2015

I got it working in a backwards compat way allowing lifetimes other than 'static. 0.4.14 on crates.io (or master branch here) should be working for you now. Let me know if that fixed what you needed. And I like your example for auto-generating the version from the Cargo.toml, I'm gonna throw that in the examples dir. Thanks again 👍

@jhelwig
Copy link
Author

jhelwig commented Mar 24, 2015

Yup, works like a charm. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants