Why? Changed the binding for some RefCell functions, but not for all?

I noticed that ?Sized is a binding to a parameter of type T for some functions ( borrow , borrow_state and borrow_mut ), but not a for new or into_inner . If I cannot create a RefCell containing dynamically size ( RefCell<T : ?Sized> ), then what good is it that there are functions that can work on such a thing?

+5
source share
1 answer

This support was added to the commit, which also added tests . We can look at these tests to see how it should be used:

 use std::cell::RefCell; #[test] fn refcell_unsized() { let cell: &RefCell<[i32]> = &RefCell::new([1, 2, 3]); { let b = &mut *cell.borrow_mut(); b[0] = 4; b[2] = 5; } let comp: &mut [i32] = &mut [4, 2, 5]; assert_eq!(&*cell.borrow(), comp); } 

You always need to have a constructor with a Sized binding, since the compiler needs to know the amount of space to put on the stack. After that, you can force the type of dynamic size.

+4
source

Source: https://habr.com/ru/post/1242563/


All Articles