How does this Box conversion to Rc> work?

⚓ Rust    📅 2025-08-12    👤 surdeus    👁️ 5      

surdeus

let set_pan_position: Rc<RefCell<dyn FnMut(&Vec2f64)>> =
    Rc::new(RefCell::new(pan.set_position));

where pan.set_position has type:

pub set_position: Box<dyn FnMut(&Vec2f64), Global>

Hello! How does the above conversion work? Alternatives that DON'T work:

  • pan.set_position is unsized, so it can't be dereferenced in order to do something like this: Rc::new(RefCell:new(*pan.set_position))
  • It's not like we're using Rc::from([some boxed value]) to make use of the fn from implementation on the screenshot below:
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Rc<T, A> {
    /// Move a boxed object to a new, reference counted, allocation.
    ///
    /// # Example
    ///
    /// ```
    /// # use std::rc::Rc;
    /// let original: Box<i32> = Box::new(1);
    /// let shared: Rc<i32> = Rc::from(original);
    /// assert_eq!(1, *shared);
    /// ```
    #[inline]
    fn from(v: Box<T, A>) -> Rc<T, A> {
        Rc::from_box_in(v)
    }
}

5 posts - 3 participants

Read full topic

🏷️ Rust_feed