use core::convert::Infallible;
use core::num::NonZeroUsize;
use qubit_codec as codec;
use qubit_codec::Codec;
#[derive(Default)]
pub(crate) struct ResetSensitiveLifecycleCodec {
decode_state: usize,
}
impl Codec for ResetSensitiveLifecycleCodec {
type Value = u8;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = 1;
const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;
const MAX_DECODE_UNITS_PER_VALUE: usize = 1;
const MAX_DECODE_RESET_VALUES: usize = 1;
const MAX_DECODE_FINISH_VALUES: usize = 1;
unsafe fn decode_reset(&mut self, output: &mut [u8], output_index: usize) -> Result<usize, Self::DecodeError> {
output[output_index] = 0xfe;
self.decode_state = 1;
Ok(1)
}
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<(u8, NonZeroUsize), codec::DecodeFailure<Self::DecodeError>> {
let decoded = input[input_index].wrapping_sub(self.decode_state as u8);
self.decode_state += 1;
Ok((decoded, NonZeroUsize::MIN))
}
unsafe fn encode(
&mut self,
value: &u8,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
output[output_index] = *value;
Ok(1)
}
unsafe fn decode_finish(&mut self, output: &mut [u8], output_index: usize) -> Result<usize, Self::DecodeError> {
output[output_index] = self.decode_state as u8;
self.decode_state = 0;
Ok(1)
}
}