nostd_bv/
array_n_impls.rs

1//! This module impls the `Bits`, `BitsMut` and `BitSliceable` traits
2//! for fixed-sized arrays of `BlockType`s.
3
4use crate::{BitSliceable, Bits, BitsMut, BlockType};
5
6macro_rules! impl_traits_for_array {
7    (
8        $( $size:tt )+
9    ) => {
10        $(
11            impl<Block: BlockType> Bits for [Block; $size] {
12                type Block = Block;
13
14                fn bit_len(&self) -> u64 {
15                    Block::mul_nbits(self.block_len())
16                }
17
18                fn block_len(&self) -> usize {
19                    $size
20                }
21
22                fn get_block(&self, position: usize) -> Self::Block {
23                    self[position]
24                }
25            }
26
27            impl<Block: BlockType> BitsMut for [Block; $size] {
28                fn set_block(&mut self, position: usize, value: Block) {
29                    self[position] = value;
30                }
31            }
32
33            impl<'a, R, Block: BlockType> BitSliceable<R> for &'a [Block; $size]
34                where &'a [Block]: BitSliceable<R, Block = Block> {
35
36                type Slice = <&'a [Block] as BitSliceable<R>>::Slice;
37
38                fn bit_slice(self, range: R) -> Self::Slice {
39                    (self as &'a [Block]).bit_slice(range)
40                }
41            }
42
43            impl Bits for [bool; $size] {
44                type Block = u8;
45
46                fn bit_len(&self) -> u64 {
47                    $size
48                }
49
50                fn get_bit(&self, position: u64) -> bool {
51                    self[position as usize]
52                }
53            }
54
55            impl BitsMut for [bool; $size] {
56                fn set_bit(&mut self, position: u64, value: bool) {
57                    self[position as usize] = value;
58                }
59            }
60
61            impl<'a, R> BitSliceable<R> for &'a [bool; $size]
62                where &'a [bool]: BitSliceable<R, Block = u8> {
63
64                type Slice = <&'a [bool] as BitSliceable<R>>::Slice;
65
66                fn bit_slice(self, range: R) -> Self::Slice {
67                    (self as &'a [bool]).bit_slice(range)
68                }
69            }
70        )+
71    };
72}
73
74impl_traits_for_array! {
75    0 1 2 3 4 5 6 7
76    8 9 10 11 12 13 14 15
77    16 17 18 19 20 21 22 23
78    24 25 26 27 28 29 30 31
79    32 64 128 256 512 1024 2048 4096
80    8_192 16_384 32_768 65_536 131_072 262_144 524_288 1_048_576
81}