Closed Kmeakin closed 1 year ago
Unfortunately, it is not possible in the current design because EcoVec::new
depends on a static and const fns can't reference statics.
Vec
initializes its pointer with NonNull::dangling
and uses its capacity
to track whether it is allocated. EcoVec
in comparison initializes its pointer with the address of a static SENTINEL
value and uses that to track whether it is allocated. Using NonNull::dangling
isn't possible because we have no other means to track whether we are allocated and NonNull::dangling
could be a valid pointer to an allocation.
Null cannot be used in either case because a slice's pointer can't be null and we want deref to slice to be a no-op (that's a design goal).
Could the sign bit in len
be used as the allocated flag? Allocations of more than isize::MAX
are not allowed in Rust, so self.len > (isize::MAX as usize)
could be used to signal an unallocated EcoVec
Then EcoVec<T>
's deref to [T]
isn't a no-op once again. Moreover, EcoString
(which uses EcoVec
internally) uses that bit to distinguish between inline and heap (EcoVec<u8>
) representation.
Possible implementation: https://github.com/typst/ecow/pull/28
Implemented in https://github.com/typst/ecow/pull/28.
I'll publish a new release soon.
Sorry for the ping, any news about the release?
@flyingmutant I'll make one later today.
@flyingmutant I've just released ecow 0.1.2.
It would be nice if
EcoVec::new()
were callable inconst
contexts, to matchVec::new()