let foo = span!(1, 1);
let bar = span!(1, 1, "file.txt");
However, sometimes I don't want to pass in the file path directly but through a variable that is Option<String>. To do this, I always have to match the variable:
let file_path = Some("file.txt");
let foo = match file_path {
Some(file_path) => span!(1, 1, file_path),
None => span!(1, 1),
}
Is there a way which allows me to directly use span!(1, 1, file_path) where file_path could be "file.txt", Some("file.txt") or None?
Option<T> has a From<T> implementation that lets you write Option::from($file_path).map(|path| path.to_string()) to accept both cases in the same expression.
Imagine you want to turn a Into<String> to Some(val.into()) and Option<Into<String>> to val.map(Into::into).
Now, what if there is a type T where impl From <Option<T>> for String is implemented?
Then we would have a conflict.
If you only need this for &str and String, then you can add a wrapper type OptionStringWrapper(Option<String>) and implement From<T> for OptionStringWrapper for all concrete type cases you want to support, and go from there.