Skip to content

Sequence-Like data-structure for storing dynamically sized types

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

HellButcher/dynsequence-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dynsequence

Crates.io docs.rs license: MIT/Apache-2.0 Rust CI

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))

# #[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_eq!(Some(&1234), seq.get(1).and_then(|a| a.downcast_ref()));
assert_eq!(Some(&"foo"), seq.get(0).and_then(|a| a.downcast_ref()));
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

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_eq!(Some(&1234), seq.get(1).and_then(|a| a.downcast_ref()));
assert_eq!(Some(&"foo"), seq.get(0).and_then(|a| a.downcast_ref()));
assert!(seq.get(2).is_none());

// push with macro hack
dyn_sequence![dyn Any | &mut seq => {
  push (true);
} ];
assert_eq!(Some(&true), seq.get(2).and_then(|a| a.downcast_ref()));

no_std

This crate should also work without std (with alloc). No additional configuration required.

License

This project is licensed under either of

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.

About

Sequence-Like data-structure for storing dynamically sized types

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages