typed-index-collections
The typed-index-collections crate provides TiSlice and TiVec structs
that are typed index versions of the Rust slice and std::vec::Vec types.
Introduction
When dealing with slices and vectors it is not safe to index all arrays
with type-unsafe usizes as indices.
Using slice and vector indexing might be useful in Data Oriented Design,
when using Struct of Arrays, or when Rc and Weak usage is undesirable.
About
This crate provides typed index version of slice and std::vec::Vec
types with custom index type.
Containers only require the index to implement Index trait.
If default feature impl-index-from is enabled, this trait is automatically implemented
when From<usize> and Into<usize> are implemented.
And their implementation can be easily done
with derive_more crate and #[derive(From, Into)].
The TiSlice and TiVec structs are only wrappers
around Rust slice and std::vec::Vec structs with custom index type
and exposed raw property with original struct.
They can be easily converted to matched Rust containers using From and Into traits.
The structs mirrors the stable API of Rust slice and std::vec::Vec
and forwards to it as much as possible.
Usage
First, add the following to your Cargo.toml:
[]
= "0.0.3"
This crate depends on the standard library by default that is useful
for debugging and for some extra functionality.
To use this crate in a #![no_std] context, use default-features = false
in your Cargo.toml as shown below:
[]
= "0.0.3"
= false
= ["alloc", "impl-index-from"]
If you want to use derive_more for From<usize> and Into<usize> implementation
add it to your Cargo.toml as shown below:
[]
= "0.99"
= "0.0.3"
Examples
Simple example with derive_more:
use TiVec;
use ;
;
let mut foos: = vec!.into;
foos.insert;
assert_eq!;
Another example with derive_more:
use ;
use ;
;
;
let first = Foo ;
let second = Foo ;
let slice_ref = &;
let vec = vec!;
let boxed_slice = vec!.into_boxed_slice;
let typed_index_slice_ref: & = slice_ref.into;
let typed_index_vec: = vec.into;
let typed_index_boxed_slice: Box = boxed_slice.into;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// assert_eq!(vec[BarId(1)], second); // won't compile with incompable index
let _slice_ref: & = typed_index_slice_ref.into;
let _vec: Vec = typed_index_vec.into;
let _boxed_slice: Box = typed_index_boxed_slice.into;
Documentation
Feature Flags
impl-index-from(enabled by default) — Enables automaticIndextrait implementation for types that implementFrom<usize>andInto<usize>.alloc(enabled by default) — Enables types and functions which require memory allocation.std(enabled by default) — Enables allstdfeatures such as memory allocations andstd::error::Errortrait.serde— ImplementsSerializeandDeserializetraits for slice type.serde-alloc— Enableallocandserde/allocfeatures.serde-std— Enablestdandserde/stdfeatures.
Similar crates
typed_index_collectionprovides aVecwrapper with a very limited API. Indices are u32 wrappers, they are not customizable and can only index a specific type of container.indexed_vecis the closest copy of theIndexVecstruct fromlibrustc_index, but API is also different from standard Ruststd::vec::Vecand it has no typed indexslicealternative.index_vechave bothsliceandstd::vec::Vecwrapper and API closer to standard API. But it implicitly allows you to useusizefor get methods and index expressions that reduce type-safety, and the macrodefine_index_type!which is used to generate a newtyped index struct, implicitly implements a lot of traits that would be better implemented only when necessary using crates intended for this, such asderive_more.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.