I'm migrating from clap v2.33 to clap v3.15 and stuck with the following lifetime error.
Original code (clap 2.33):
use clap::{App, Arg};
use core::fmt::Display;
fn val<T>(string: T) -> Result<(), String>
where
T: AsRef<str> + Display,
{
if string.as_ref().len() > 4 {
return Ok(());
} else {
return Err("banana".to_string());
}
}
fn is_validator<T>(string: T) -> Result<(), String>
where
T: AsRef<str> + Display,
{
val(string)
}
pub fn fee_payer_arg<'a, 'b>() -> Arg<'a, 'b> { // two lifetimes, but don't get where they come
Arg::with_name("NAME")
.long("name")
.validator(is_validator)
}
New code (v3.15)
use clap::{Command, Arg};
use core::fmt::Display;
fn val<T>(string: T) -> Result<(), String>
where
T: AsRef<str> + Display,
{
if string.as_ref().len() > 4 {
return Ok(());
} else {
return Err("banana".to_string());
}
}
fn is_validator<T>(string: T) -> Result<(), String>
where
T: AsRef<str> + Display,
{
val(string)
}
pub fn fee_payer_arg<'a>() -> Arg<'a> {
Arg::new("NAME")
.long("name")
.validator(is_validator) // error here
}
The error is quite long:
error[E0308]: mismatched types
--> src/main.rs:62:10
|
62 | .validator(is_validator)
| ^^^^^^^^^ lifetime mismatch
|
= note: expected type `<fn(&str) -> Result<(), std::string::String> {is_validator::<&str>} as FnOnce<(&str,)>>`
found type `<fn(&str) -> Result<(), std::string::String> {is_validator::<&str>} as FnOnce<(&str,)>>`
note: the required lifetime does not necessarily outlive the lifetime `'a` as defined here
--> src/main.rs:59:22
|
59 | pub fn fee_payer_arg<'a>() -> Arg<'a> {
| ^^
note: the lifetime requirement is introduced here
--> /home/klykov/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-3.1.6/src/build/arg.rs:1535:27
|
1535 | F: FnMut(&str) -> Result<O, E> + Send + 'help,
| ^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `args` due to previous error