tact1m4n3 / crsf-rs

A work in progress crsf protocol packet parser
MIT License
10 stars 7 forks source link

Faster parsing of RcChannels using raw byte manipulation #18

Closed peterkrull closed 4 months ago

peterkrull commented 4 months ago

Solves #15

Gives a nice speed-up to the parsing, and gets rid of a dependency.

peterkrull commented 4 months ago

I am not stoked about using unwrap or doing the value clamping, but that is what I feel is necessary when there is no way to fail the operation.

Something I would probably revise is the Payload trait to make parsing and dumping fallible, and then probably not rely on dynamic dispatching within the library, since that would not be compatible with this iteration of the trait.

I was thinking something like

pub(crate) trait Payload
where
    Self: Sized,
{
    const LEN: usize;

    const fn packet_type(&self) -> PacketType;
    fn parse(buf: &[u8]) -> Result<Self, PacketError>;
    fn dump(&self, buf: &mut [u8]) -> Result<&[u8], PacketError>;
}
tact1m4n3 commented 4 months ago

Sorry for overlooking the second message :))

let data = mut_array_start::<{ Self::PAYLOAD_LENGTH as usize }>(data).unwrap();

I think this is fine since there is no way for it to fail(we check for that in the Packet's dump method).

But for value clamping or any kind of invalid packet payload we could make an error(this would mean dump should return a result).