- As J_H mentioned, there are more efficient ways to compute
log2of au32-31 - n.leading_zeros()should do.As J_H mentioned, there are more efficient ways to compute
log2of au32-31 - n.leading_zeros()should do. base.pow()withbase = 2should really be1 << bits. Are you really intending to use anotherbasevalue in the future?base.pow()withbase = 2should really be1 << bits. Are you really intending to use anotherbasevalue in the future?- The two derive macros can be combined in one line and probably also should include
Default-#[derive(Copy, Clone, Default)], then you can also writeNode::default()instead ofNode { expanded: false, filled: false }. Especially in case you add additional properties in the future this makes it easier.The two derive macros can be combined in one line and probably also should include
Default-#[derive(Copy, Clone, Default)], then you can also writeNode::default()instead ofNode { expanded: false, filled: false }. Especially in case you add additional properties in the future this makes it easier. Consider using
usizeeverywhere since your dealing with memory management anyway. Removes the need for a bunch of casts.Since
usizetype size depends on the platform, the improvedlog2implementation would then have to be something likestd::mem::size_of::<usize>() * 8 - 1 - (n.leading_zeros() as usize)
While a sentinel value to indicate failure is a common programming tool, in Rust functions that can fail should return
Result<>so that they can return an actual error and not pick some (more or less arbitrary value) as error indicator. Returning a proper error also let's you include additional details as to what the failure specifically is.The reason why you currently have
unsafeis because you have a global static mutable array. The reason it's unsafe is because it's not thread-safe. Pretty sure that you can do away with it by using a combination oflazy_static(or apparentlyonce_cellin newer compiler versions) andMutex. The recursive nature of the implementation might be problematic though in this case.