|
2 | 2 |
|
3 | 3 | ## [Unreleased]
|
4 | 4 |
|
| 5 | +## Changes |
| 6 | +[#352](https://github.com/sharksforarms/deku/pull/352) replaced the byte slice interface with new interfaces depending on `io::Read`. |
| 7 | +This brings massive performance and usability improvements. |
| 8 | +- `from_bytes(..)` was replaced with `from_reader(..)`: |
| 9 | + |
| 10 | +old: |
| 11 | +```rust |
| 12 | +#[derive(Debug, PartialEq, DekuRead, DekuWrite)] |
| 13 | +struct TestDeku(#[deku(bits = 4)] u8); |
| 14 | + |
| 15 | +let test_data: Vec<u8> = [0b0110_0110u8, 0b0101_1010u8].to_vec(); |
| 16 | + |
| 17 | +let ((rest, i), ret_read) = TestDeku::from_bytes((&test_data, 0)).unwrap(); |
| 18 | +assert_eq!(TestDeku(0b0110), ret_read); |
| 19 | +assert_eq!(2, rest.len()); |
| 20 | +assert_eq!(4, i); |
| 21 | +``` |
| 22 | + |
| 23 | +new: |
| 24 | +```rust |
| 25 | +#[derive(Debug, PartialEq, DekuRead, DekuWrite)] |
| 26 | +struct TestDeku(#[deku(bits = 4)] u8); |
| 27 | + |
| 28 | +let test_data: Vec<u8> = [0b0110_0110u8, 0b0101_1010u8].to_vec(); |
| 29 | +let mut c = std::io::Cursor::new(test_data); |
| 30 | + |
| 31 | +let (amt_read, ret_read) = TestDeku::from_reader((&mut c, 0)).unwrap(); |
| 32 | +assert_eq!(TestDeku(0b0110), ret_read); |
| 33 | +assert_eq!(amt_read, 4); |
| 34 | +``` |
| 35 | + |
| 36 | +- The more internal (with context) `read(..)` was replaced with `from_reader_with_ctx(..)`. |
| 37 | +Now unused variables `deku::input`, `deku::input_bits`, and `deku::rest` were removed. `deku::reader` is the replacement. |
| 38 | + |
| 39 | +old: |
| 40 | +```rust |
| 41 | +#[derive(Debug, PartialEq, DekuRead, DekuWrite)] |
| 42 | +struct DekuTest { |
| 43 | + field_a: u8, |
| 44 | + |
| 45 | + #[deku( |
| 46 | + reader = "bit_flipper_read(*field_a, deku::rest, BitSize(8))", |
| 47 | + )] |
| 48 | + field_b: u8, |
| 49 | +} |
| 50 | + |
| 51 | +fn custom_read( |
| 52 | + field_a: u8, |
| 53 | + rest: &BitSlice<u8, Msb0>, |
| 54 | + bit_size: BitSize, |
| 55 | +) -> Result<(&BitSlice<u8, Msb0>, u8), DekuError> { |
| 56 | + |
| 57 | + // read field_b, calling original func |
| 58 | + let (rest, value) = u8::read(rest, bit_size)?; |
| 59 | + |
| 60 | + Ok((rest, value)) |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +new: |
| 65 | +```rust |
| 66 | +#[derive(Debug, PartialEq, DekuRead, DekuWrite)] |
| 67 | +struct DekuTest { |
| 68 | + field_a: u8, |
| 69 | + |
| 70 | + #[deku( |
| 71 | + reader = "bit_flipper_read(*field_a, deku::reader, BitSize(8))", |
| 72 | + )] |
| 73 | + field_b: u8, |
| 74 | +} |
| 75 | + |
| 76 | +fn custom_read<R: std::io::Read>( |
| 77 | + field_a: u8, |
| 78 | + reader: &mut Reader<R>, |
| 79 | + bit_size: BitSize, |
| 80 | +) -> Result<u8, DekuError> { |
| 81 | + |
| 82 | + // read field_b, calling original func |
| 83 | + let value = u8::from_reader_with_ctx(reader, bit_size)?; |
| 84 | + |
| 85 | + Ok(value) |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +- With the addition of using `Read`, containing a byte slice with a reference is not supported: |
| 90 | + |
| 91 | +old |
| 92 | +```rust |
| 93 | +#[derive(PartialEq, Debug, DekuRead, DekuWrite)] |
| 94 | +struct TestStruct<'a> { |
| 95 | + bytes: u8, |
| 96 | + |
| 97 | + #[deku(bytes_read = "bytes")] |
| 98 | + data: &'a [u8], |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +new |
| 103 | +```rust |
| 104 | +#[derive(PartialEq, Debug, DekuRead, DekuWrite)] |
| 105 | +struct TestStruct { |
| 106 | + bytes: u8, |
| 107 | + |
| 108 | + #[deku(bytes_read = "bytes")] |
| 109 | + data: Vec<u8>, |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +- `id_pat` is now required to be the same type as stored id. |
| 114 | +This also disallows using tuples for storing the id: |
| 115 | + |
| 116 | +old: |
| 117 | +```rust |
| 118 | +#[derive(PartialEq, Debug, DekuRead, DekuWrite)] |
| 119 | +#[deku(type = "u8")] |
| 120 | +enum DekuTest { |
| 121 | + #[deku(id_pat = "_")] |
| 122 | + VariantC((u8, u8)), |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +new: |
| 127 | +```rust |
| 128 | +#[derive(PartialEq, Debug, DekuRead, DekuWrite)] |
| 129 | +#[deku(type = "u8")] |
| 130 | +enum DekuTest { |
| 131 | + #[deku(id_pat = "_")] |
| 132 | + VariantC { |
| 133 | + id: u8, |
| 134 | + other: u8, |
| 135 | + }, |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +- The feature `const_generics` was removed and is enabled by default. |
| 140 | + |
5 | 141 | ## [0.16.0] - 2023-02-28
|
6 | 142 |
|
7 | 143 | ### Changes
|
|
0 commit comments