futures_intrusive/buffer/real_array.rs
1/// A marker trait which may only be implemented for native array types, like
2/// `[T; 2]`. The library incorporates several components that are parameterized
3/// by array types, but currently Rust provides no safe mechanism to express
4/// that.
5///
6/// In order to work around the limitations, these methods only accept arrays
7/// which implement the `RealArray` type. The library provides an implementation
8/// of `RealArray` for arrays up to length 64, as well as for all powers of 2
9/// up to 64k.
10///
11/// In order to let the library accept arrays of bigger sizes, `RealArray` can
12/// be implemented by users via newtypes. A type as defined in the following
13/// example can be passed to the library:
14///
15/// ```
16/// use futures_intrusive::buffer::RealArray;
17/// use futures_intrusive::channel::LocalChannel;
18///
19/// struct I32x384Array([i32; 384]);
20/// unsafe impl RealArray<i32> for I32x384Array {
21/// const LEN: usize = 384;
22/// }
23///
24/// impl AsMut<[i32]> for I32x384Array {
25/// fn as_mut(&mut self) -> &mut [i32] {
26/// &mut self.0
27/// }
28/// }
29///
30/// impl AsRef<[i32]> for I32x384Array {
31/// fn as_ref(&self) -> &[i32] {
32/// &self.0
33/// }
34/// }
35///
36/// fn main() {
37/// let channel = LocalChannel::<i32, I32x384Array>::new();
38/// }
39///
40/// ```
41pub unsafe trait RealArray<T> {
42 /// The length of the array
43 const LEN: usize;
44}
45
46macro_rules! real_array {
47 ($($N:expr),+) => {
48 $(
49 unsafe impl<T> RealArray<T> for [T; $N] {
50 const LEN: usize = $N;
51 }
52 )+
53 }
54}
55
56real_array!(
57 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
58 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
59 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
60 59, 60, 61, 62, 63, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
61 32768, 65536
62);