Crates.io | sashay |
lib.rs | sashay |
version | 0.5.0 |
source | src |
created_at | 2022-11-08 19:31:24.207329 |
updated_at | 2023-01-26 18:19:19.925095 |
description | Type-erased and lifetime-erased references and slices |
homepage | https://github.com/stijnfrishert/sashay |
repository | https://github.com/stijnfrishert/sashay |
max_upload_size | |
id | 708251 |
size | 57,790 |
Sashay contains type-erased slices and references that work kinda like Any
, but not entirely:
&'a T
-> AnyRef<'a>
&'a mut T
-> AnyMut<'a>
&'a [T]
-> AnySliceRef<'a>
&'a mut [T]
-> AnySliceMut<'a>
The big advantage of these types if that you can deal with references and slices of any type without having to resort to generic code. Perhaps more importantly, it allows you to store them in homogeneous containers without having to use trait objects (which is what I originally wrote this for).
Any of these refs and muts can be constructed by calling ::erase()
on a reference or slice. The erased types are still lifetime-bound, and they also contains a TypeId
to check if any unerasure is valid. Internally the structures hold pointers to the original data.
You could AnyRef/Mut
to erase [T]
slices, but AnySliceRef/Mut
retain part of the expected API for primitive slices, such as calling .len()
or .is_empty()
and providing access to subslices or individual elements.
As far as I know the library is sound and it passes cargo miri test
, but outside of personal use it is untested in the wild. I have chatted with people in the Rust Zulip (big thanks to Lokathor, Ben Kimock, Mario Carneiro and scottmcm) to cover edge cases. Feedback is always appreciated.
And last but not least: don't forget to enjoy your day! ;)
let data : [i32; 3] = [0, 1, 2];
// Type-erase a slice
let erased = sashay::AnySliceRef::erase(data.as_slice());
assert_eq!(erased.len(), 3);
// Unerase the whole slice
let unerased = erased.unerase::<i32>().expect("any was not a &[i32]");
assert_eq!(unerased, data.as_slice());
// Unerase just a single element
assert_eq!(erased.get(2).unwrap().unerase::<i32>(), Some(&2));
sashay
is #![no_std]
and has 0 dependencies.