Hi! I tried to make the following code generic. The goal is to make the output reference of a Uri live as long as possible, while reducing repetitive code.
struct Uri<T> {
storage: T,
// ...Parsed component bounds, etc.
}
impl<T> Uri<T> {
fn parse(storage: T) -> Uri<T> {
Uri { storage }
}
}
impl<'a> Uri<&'a str> {
fn as_str(&self) -> &'a str {
self.storage
}
}
impl Uri<String> {
fn as_str(&self) -> &str {
&self.storage
}
}
// these are the desired behavior:
fn ref_outlives_borrowed_uri(s: &str) -> &str {
Uri::parse(s).as_str()
}
fn ref_does_not_outlive_owned_uri() -> &'static str {
// error: cannot return value referencing temporary value
Uri::parse(String::new()).as_str()
}


