The indexed::Pool
, a convenient allocator for index-linked data structures.
Features
-
Vec
-like operations.
Supportspush()
,reserve()
and random access by indexes which are similar withstd::Vec
's methods.
An unsafewrite()
method is provided, similar withstd::ptr::write()
except using index instead of pointer. -
Using indexes to link elements to each other.
Any element in the pool must implementindexed::Indexed
, which stores its index in itself. A user-definednull()
index indicates an empty linkage. -
Obtaining reference of the pool from any one of its elements.
This feature makes it possible to simply use reference of element instead of the style of using pool + index. It is convenient in some usecases because the library users do not need to store/pass the references of the pool everywhere.
NOTICE: this feature is unsafe and it is the duty of the user not to violating memory safety. -
No reallocation will happen.
Once an element is located in the pool, it will not move at all. -
The pool can be managed or unmanaged.
A managed pool owns its elements and drops them in destruction while an unmanaged pool does not.
Performance
-
The underlying buffers are not continuous but segmented
Vec
s. Mapping conceptual index to underlying buffer address is as lightweight as doing one integer division. -
The elements should provide space for storing its index. Index stored in usize occupies one extra pointer size. Index stored in u32 may occupy no extra space if some 32-bit hole exists in the struct in order to meet the alignment requirement.
-
Obtaining the pool's reference from its element is as efficient as one pointer arithmetic and deference operation. Library users can pick the classic pool + index API style if not satisfied with this overhead.
-
Library users can pick up a different chunk size other than the default 256 for performance.
License
Licensed under MIT.
Example
See API doc for more.