@@ -47,15 +47,11 @@ where
4747 node_size : usize ,
4848 num_items : usize ,
4949 level_bounds : Box < [ usize ] > ,
50- /// boxes holds the tree data (all nodes and items)
5150 #[ cfg( feature = "unsafe_optimizations" ) ]
5251 boxes : Box < [ std:: mem:: MaybeUninit < AABB < T > > ] > ,
5352 #[ cfg( not( feature = "unsafe_optimizations" ) ) ]
5453 boxes : Box < [ AABB < T > ] > ,
55- /// indices is used to map from sorted indices to indices ordered according to the order items
56- /// were added
5754 indices : Box < [ usize ] > ,
58- // used to keep track of the current position for boxes added
5955 pos : usize ,
6056}
6157
@@ -106,14 +102,14 @@ where
106102 node_size : usize ,
107103 num_items : usize ,
108104 level_bounds : Box < [ usize ] > ,
109- /// boxes holds the tree data (all nodes and items)
110105 boxes : Box < [ AABB < T > ] > ,
111- /// indices is used to map from sorted indices to indices ordered according to the order items
112- /// were added
113106 indices : Box < [ usize ] > ,
114107}
115108
116- // get_at_index and set_at_index helper functions to toggle bounds checking at compile time
109+ // Helper functions to toggle bounds checking/uninitialized memory handling. NOTE: the functions are
110+ // not marked unsafe to facilitate easy global usages since we never rely on these functions
111+ // throwing a panic (so with unsafe_optimizations feature on we assume correct bounds and
112+ // initialization).
117113#[ cfg( not( feature = "unsafe_optimizations" ) ) ]
118114#[ inline( always) ]
119115fn get_at_index < T > ( container : & [ T ] , index : usize ) -> & T {
@@ -315,6 +311,7 @@ where
315311 }
316312
317313 #[ cfg( feature = "unsafe_optimizations" ) ]
314+ // SAFETY: All the item boxes are initialized (all elements from index 0 to num_items - 1).
318315 let item_boxes: & mut [ AABB < T > ] =
319316 unsafe { std:: mem:: transmute ( & mut self . boxes [ 0 ..self . num_items ] ) } ;
320317
@@ -350,8 +347,12 @@ where
350347 ) ;
351348
352349 #[ cfg( feature = "unsafe_optimizations" ) ]
350+ // SAFETY: All boxes are initialized.
353351 let boxes: Box < [ AABB < T > ] > = unsafe { std:: mem:: transmute ( self . boxes ) } ;
354352
353+ #[ cfg( not( feature = "unsafe_optimizations" ) ) ]
354+ let boxes = self . boxes ;
355+
355356 return Ok ( StaticAABB2DIndex {
356357 node_size : self . node_size ,
357358 num_items : self . num_items ,
@@ -441,8 +442,12 @@ where
441442 }
442443
443444 #[ cfg( feature = "unsafe_optimizations" ) ]
445+ // SAFETY: All boxes are initialized.
444446 let boxes: Box < [ AABB < T > ] > = unsafe { std:: mem:: transmute ( self . boxes ) } ;
445447
448+ #[ cfg( not( feature = "unsafe_optimizations" ) ) ]
449+ let boxes = self . boxes ;
450+
446451 Ok ( StaticAABB2DIndex {
447452 node_size : self . node_size ,
448453 num_items : self . num_items ,
0 commit comments