This crate provides 2 kinds of reference: immutable and mutable.
All of them represented in one enum RefKind
, which allows to store immutable and mutable references together.
But the most importantly, this crate allows to retrieve many mutable references out of the collection by creating a new collection which holds these references.
For that very case, crate defines Many
trait which is implemented
for arrays and slices of Option<RefKind<'a, T>>
elements.
But nothing stops you to implement this trait for other collections as well!
```rust use core::array;
use ref_kind::{Many, RefKind};
// Create an array of square of integers from 0 to 9 let mut array: [; 10] = array::fromfn(|i| i * i);
// Create vector of mutable references on all of the array elements
let mut many = array
.iter_mut()
.map(|r#mut| Some(RefKind::Mut(r#mut)))
.collect::
// Move out mutable reference by index 1 // It is no longer in the vector let one = many.movemut(1).unwrap(); asserteq!(*one, 1);
// Move out immutable reference by index 4 // Vector now contains immutable reference, not mutable one let four = many.moveref(4).unwrap(); asserteq!(*four, 16); // Move it again: no panic here because immutable reference was copied let fouragain = many.moveref(4).unwrap(); asserteq!(four, fouragain);
// This call will return an error because vector contains no reference by index 1 let oneagain = many.trymoveref(1); assert!(oneagain.is_err()); ```
This crate used to be the part of toucan_ecs
crate,
but now was moved into the separate crate!
#![no_std]
supportThis crate is a no_std
crate. It depends only on the core
crate.
#![forbid(unsafe_code)]
This crate contains no unsafe
code.
Licensed under either of
at your option.