Open sosthene-nitrokey opened 1 year ago
Types:
Vec
String
Deque
LinearMap
HistoryBuffer
BinaryHeap
MpMcQueue
SortedLinkedList
Idx
generic at the same time, though it would likely be a challenge.spsc::Queue
IndexMap
and IndexSet
IndexSet
depends on the View
type for IndexMap
being available.
IndexMap
is currently implemented with a CoreMap
type that looks like:
struct CoreMap<K, V, const N: usize> {
entries: Vec<Bucket<K, V>, N>,
indices: [Option<Pos>; N],
}
It is not possible to construct a View
for this type like for Vec
since the N
would have to be erased twice.
We could solve that issue by "inlining" the indices into the entries:
struct CoreMap<K, V, const N: usize> {
entries_indices: [(MaybeUninit<Bucket<K, V>>, Option<Pos>); N],
len: usize,
}
Then only one N
needs to be erased, and this can be implemented. However this requires changing the implementation significantly.
It might even be possible to get rid of the len
field (I think an entry is initialized if and only if a index points to it, and only one index can point to it at a given time?).
There are cases where some adjacent structures have the const generic that could be removed thanks to this implementation. Would it be worth making breaking changes to remove this const generic?
Making code flexible around heapless structures can be a bit verbose because it requires being generic over the capacity of each collection. This could be helped by providing versions of the structures with the
const
erased. This would allow removing the generics in some cases.For example,
heapless::Vec
currently is :It should be possible to provide a struct that goes hand in hand with it:
With
VecView
implementing most of the API that Vec currently implements. This would allow for example methods that write data to an outbuffer of type&mut Vec<T, N>
to instead take a parameterVecView<'_, T>
.Something like:
could become
In a trait for example this would also have the advantage of making it object-safe.
I believe this pattern: