Explicitly using String::from
in the following code works, but how can I make it automatically use From<OsStringWrap<'a>>
trait without explicitly using String::from
?
use serde::Serialize; // 1.0.115
struct OsStringWrap<'a>(&'a std::ffi::OsString);
impl<'a> From<OsStringWrap<'a>> for String {
fn from(s: OsStringWrap) -> String {
s.0.to_string_lossy().to_string()
}
}
pub fn insert<T: Serialize + ?Sized, S: Into<String>>(_key: S, _value: &T) {}
fn main() {
for (key, value) in std::env::vars_os() {
// HOW-TO: auto use From<OsStringWrap<'a>> trait
// without explicit `String::from` like below?
/*
insert(OsStringWrap(&key), &OsStringWrap(&value))
*/
// below using `String::from` to make it explicitly
// but want to find a way to make it shorter
insert(OsStringWrap(&key), &String::from(OsStringWrap(&value)))
}
}
Playground, and the insert
method is a real case from tera