Looking through the code you posted, do you need to use references? It looks like you are using them to store references to functions like &Fn(T) -> U
, but you could just use normal function pointers, fn(T) -> U
, if you don't need to capture anything (which seems to be the case). Also, avoid putting references in struct
s, it really messes up the types and is really hard to work with, as it infects everything.
Also, you could rewrite stuff like access_flattened_vec
to return an index, instead of accessing the slice directly.
pub fn coord_to_index(
coords: &[isize],
dimensions: &[usize],
offset: usize,
) -> usize { /* your code here */ }
Because std::cell::Ref
implements Deref
, &'_ Ref<'_, T>
is redundant, instead just do &'_ T
.
Similarly, &Vec<T>
is useless, prefer a &[T]
. Note &mut Vec<T>
is not useless, because you can mutate the Vec
's length.
Why are you using RefCell
inside Grid
? More importantly, why did you put a lifetime of Grid
? It doesn't look like anything is actually binding the lifetime, so why not remove the phantom that is making the lifetime?
If you have an internal function, you can mark it pub(crate)
instead of pub
, and it will only be visible inside the crate.
minor nit
- in
AdaptiveTransitionFunctionMapNode::Link
you don't need the Box
- in
GridDataType
you have too many getters, just create 2
pub fn data(&self) -> &[CellDataType] {
&self.data
}
pub fn data_mut(&mut self) -> &mut [CellDataType] {
&mut self.data
}
If you need to turn it into a Vec
, you can use the to_vec
method on slices.
- your new functions could be cleaned up
fn new(
transition_1: &'a TransitionFunc<CellDataType, MetaDataType>,
neighbor_parser_1: &'a NeighborFuncParser<'a, 'b, CellDataType, MetaDataType>,
to_read_1: Ref<'b, GridDataType<CellDataType>>,
meta_1: &'a MetaDataType,
) -> StaticExecArgs<'a, 'b, CellDataType, MetaDataType> {
StaticExecArgs {
transition: transition_1,
neighbor_parser: neighbor_parser_1,
to_read: to_read_1,
meta: meta_1,
}
}
could be turned into
fn new(
transition: &'a TransitionFunc<CellDataType, MetaDataType>,
neighbor_parser: &'a NeighborFuncParser<'a, 'b, CellDataType, MetaDataType>,
to_read: Ref<'b, GridDataType<CellDataType>>,
meta: &'a MetaDataType,
) -> StaticExecArgs<'a, 'b, CellDataType, MetaDataType> {
StaticExecArgs {
transition,
neighbor_parser,
to_read,
meta,
}
}
Instead of this
if let Ok(a) = tmp.try_borrow() {
a
} else {
panic!("Cannot borrow data");
}
You could simply do tmp.borrow()
, which does the same thing. (Similarly for try_borrow_mut
/borrow_mut
). Although, removing the RefCell
s altogether is probably the best solution.
Looking back, I think I may have spent way too much time on this