Expand description
An implementation for a &dyn [Trait]
-like type, inspired by a Reddit thread.
Indexing into a dyn slice yields a dyn object.
§Examples
// Enable the required features (nightly must be used)
#![feature(ptr_metadata)]
use dyn_slice::declare_new_fns;
use std::fmt::Display;
// Declare the `new` function
// (or you can use `dyn_slice::standard::display::new`
declare_new_fns!(display_slice Display);
fn main() {
let array: [u8; 4] = [1, 2, 3, 4];
// Create the first dyn slice
let slice = display_slice::new(&array);
let array2: [i16; 3] = [5, 6, 7];
// Create the second dyn slice
let slice2 = display_slice::new(&array2);
// The iterators can be chained because they are iterators
// over `&dyn Display` rather than over the underlying types
let iter = slice.iter().chain(slice2.iter());
for n in iter {
println!("{n}");
}
}
// Test the example (this can be ignored)
#[test]
fn test() {
main()
}
There are more examples in the examples directory.
§Standard new dyn slice functions
There are some pre-made new functions for common traits in standard
.
Modules§
- Dyn slice
new
andnew_mut
definitions for some common traits.
Macros§
- declare_new_fnDeprecatedDEPRECATED, use
declare_new_fns
instead! - Declare
new
andnew_mut
functions for dyn slices of a trait.
Structs§
&dyn [Trait]
&mut dyn [Trait]
- Dyn slice iterator
- Mutable dyn slice iterator