# `dynsequence`
[](https://crates.io/crates/dynsequence)
[](https://docs.rs/dynsequence/)
[](#license)
[](https://github.com/HellButcher/dynsequence-rs/actions/workflows/rust.yml)
`DynSequence<dyn Trait>` is like `Vec<Box<dyn Trait>>`, but with an optimization that avoids allocations. This works by using multiple larger blocks of memory and storing their pointers in a `Vec`. This means, the items are randomly accessible, but may not lay in continues memory.
## Example
This example stores multiple values the `DynSequence` and accesses them.
(`push(...)` requires the `"unstable"` feature (**nightly only**))
```rust
# #[cfg(feature="unstable")] {
use dynsequence::DynSequence;
use std::any::Any;
let mut seq: DynSequence<dyn Any> = DynSequence::new();
seq.push("foo");
seq.push(1234);
assert!(seq.get(2).is_none());
assert_eq!(None, seq.get(0).and_then(|a| a.downcast_ref::<bool>()));
# }
```
The following example shows the usage of a macro-hac that also works on `stable`
```rust
use dynsequence::{DynSequence,dyn_sequence};
use std::any::Any;
// construct with macro hack
let mut seq: DynSequence<dyn Any> = dyn_sequence![dyn Any => "foo", 1234];
assert!(seq.get(2).is_none());
// push with macro hack
dyn_sequence![dyn Any | &mut seq => {
push (true);
} ];
```
## `no_std`
This crate should also work without `std` (with `alloc`). No additional configuration required.
## License
[license]: #license
This project is licensed under either of
- MIT license ([LICENSE-MIT] or <http://opensource.org/licenses/MIT>)
- Apache License, Version 2.0, ([LICENSE-APACHE] or <http://www.apache.org/licenses/LICENSE-2.0>)
at your option.
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.
[license-mit]: ./LICENSE-MIT
[license-apache]: ./LICENSE-APACHE