pub struct AsOption<'lt, T>(pub &'lt T);
impl<T> AsOption<'_, ::core::option::Option<T>> {
#[inline]
#[allow(dead_code)]
pub fn value(&self) -> Option<&T> {
self.0.as_ref()
}
}
impl<'lt, T> AsOption<'lt, &'lt ::core::option::Option<T>> {
#[inline]
pub fn value(&self) -> Option<&T> {
self.0.as_ref()
}
}
pub trait AsOptionFallback<T> {
fn value(&self) -> Option<&T>;
}
impl<T> AsOptionFallback<T> for AsOption<'_, T> {
#[inline]
fn value(&self) -> Option<&T> {
Some(self.0)
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! as_option {
( $e:expr $(,)? ) => {{
#[allow(unused_imports)]
use $crate::option_info::AsOptionFallback as _;
$crate::option_info::AsOption(&$e).value()
}};
}
#[cfg(test)]
mod tests {
#[test]
fn as_option_works() {
assert_eq!(Some(&true), as_option!(true));
assert_eq!(Some(&42), as_option!(42));
assert_eq!(Some(&"Hello, World!"), as_option!("Hello, World!"));
assert_eq!(Some(&()), as_option!(Some(())));
assert_eq!(Some(&5), as_option!(Some(5)));
assert_eq!(Some(&true), as_option!(Some(true)));
assert_eq!(Some(&true), as_option!(&Some(true)));
assert_eq!(None, as_option!(Option::<u32>::None));
assert_eq!(None, as_option!(Option::<bool>::None));
assert_eq!(None, as_option!(&Option::<bool>::None));
}
}