Skip to content

Instantly share code, notes, and snippets.

@qryxip
Created April 18, 2017 14:31
Show Gist options
  • Save qryxip/6f6616e773c73434b33658f746bffd25 to your computer and use it in GitHub Desktop.
Save qryxip/6f6616e773c73434b33658f746bffd25 to your computer and use it in GitHub Desktop.
use std::borrow::Cow;
trait FromRefOpt<'a, T> {
fn from_ref_opt(option: Option<&'a T>) -> Self;
}
impl<'a, T: Clone + Default> FromRefOpt<'a, T> for Cow<'a, T> {
fn from_ref_opt(option: Option<&'a T>) -> Cow<'a, T> {
if let Some(x) = option {
Cow::Borrowed(x)
} else {
Cow::Owned(T::default())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment