pub fn new(transform: Matrix, mut children: Vec<Arc<dyn Shape>>) -> Arc<Group> {
        let mut group = Arc::new(Group {
            transform,
            ..Group::default()
        });

        for child in children.iter_mut() {
            // Children needs to be unchecked as well, otherwise shapes can't be nested.
            //
            let child_parent_ref = unsafe { Arc::get_mut_unchecked(child) }.parent_mut();
            let parent_ref = Arc::clone(&group) as Arc<dyn Shape>;
            *child_parent_ref = Arc::downgrade(&parent_ref);
        }

        let group_mut = unsafe { Arc::get_mut_unchecked(&mut group) };
        group_mut.children = children;

        group
    }
// In the book, this is `add_child()`.
//
// In order to create a bidirectional tree, interior mutability is required. Since the intent is
// _not_ to have mutexes, unsafe code is used.
//
// Although an Arc can be borrowed as mutable, it's still not possible, because:
//
// - if we get the group mutable reference before the children parent set cycle, the children will
//   require an immutable borrow (in order to clone the Arc)
// - if we get the mutable reference after the children cycle, there are now multiple Arc clones,
//   so the mutable reference will fail
//
// Group mutability is required in order to add the children.
//
// There are no trivial/clean solutions to this problem; see https://users.rust-lang.org/t/is-it-possible-to-safely-build-a-read-only-thread-safe-bidirectional-tree/52759.
//
In the book, this is `add_child()`.
In order to create a bidirectional tree, interior mutability is required. Since the intent is
_not_ to have mutexes, unsafe code is used.

Although an Arc can be borrowed as mutable, it's still not possible, because.

if we get the group mutable reference before the children parent set cycle, the children will
require an immutable borrow (in order to clone the Arc)
if we get the mutable reference after the children cycle, there are now multiple Arc clones,
so the mutable reference will fail

Group mutability is required in order to add the children.


In the book this is ` add_child () ` . In order to create a bidirectional tree interior mutability is required . Since the intent is _not_ to have mutexes unsafe code is used . Although an Arc can be borrowed as mutable it ' s still not possible because . if we get the group mutable reference before the children parent set cycle the children will require an immutable borrow ( in order to clone the Arc ) if we get the mutable reference after the children cycle there are now multiple Arc clones so the mutable reference will fail Group mutability is required in order to add the children .
pub fn new(transform: Matrix, mut children: Vec<Arc<dyn Shape>>) -> Arc<Group> {
        let mut group = Arc::new(Group {
            transform,
            ..Group::default()
        });
        for child in children.iter_mut() {
            // Children needs to be unchecked as well, otherwise shapes can't be nested.
            //
            let child_parent_ref = unsafe { Arc::get_mut_unchecked(child) }.parent_mut();
            let parent_ref = Arc::clone(&group) as Arc<dyn Shape>;
            *child_parent_ref = Arc::downgrade(&parent_ref);
        }
        let group_mut = unsafe { Arc::get_mut_unchecked(&mut group) };
        group_mut.children = children;
        group
    }
pub fn new ( transform : Matrix , mut children : Vec < Arc < dyn Shape > > ) -> Arc < Group > { let mut group = Arc :: new ( Group { transform , .. Group :: default ( ) } ) ; for child in children . iter_mut ( ) { // Children needs to be unchecked as well, otherwise shapes can't be nested. // let child_parent_ref = unsafe { Arc :: get_mut_unchecked ( child ) } . parent_mut ( ) ; let parent_ref = Arc :: clone ( & group ) as Arc < dyn Shape > ; * child_parent_ref = Arc :: downgrade ( & parent_ref ) ; } let group_mut = unsafe { Arc :: get_mut_unchecked ( & mut group ) } ; group_mut . children = children ; group }
In the book, this is `add_child()`.
In the book this is ` add_child () ` .
transform
Matrix
children
Vec<Arc<dyn Shape>>