Ref<Option<T>> -> Option<Ref<T>>?

I am aware of cell.rs - source .

I have:

x: Ref<Option<T>>

I want:

y: Option<Ref<T>>

Is there a way to do this?

EDIT: Here, Ref refers to std::cell::Ref - Rust

Something like this should work:

use std::cell::{Ref,RefCell};

fn transpose_ref_opt<T>(x:Ref<'_,Option<T>>)->Option<Ref<'_,T>> {
    if x.is_none() { None }
    else { Some(Ref::map(x, |x| x.as_ref().unwrap()))}
}
4 Likes