All the crates that #Google has done for #Rust seem to be like stuff I’ve been looking for to get better control of the memory.
Especially zerocopy
is a time saver as it has all the thinkable stuff that I have used previously core::slice::from_raw_parts
and spent a lot of time thinking of all the possible safety scenarios, such as this recent one:
impl<'a> From<&'a Header> for &'a [u8] {
fn from(value: &Header) -> Self {
// SAFETY: out-of-boundary is not possible, given that the size constraint
// exists in the struct definition. The lifetime parameter links the lifetime
// of the header reference to the slice.
unsafe { from_raw_parts((value as *const Header) as *const u8, size_of::<Header>()) }
}
}
Previously I’ve had to do similar consideration in the #Enarx project. You can do these by hand but it is nice to have a common crate, which is tested by many for these risky scenarios.
Other mentionable crate from Google is tinyvec
, which I’m going to use in zmodem2
to remove internal heap usage.
My crate has the implementation now in 933 lines and not a lot of dependencies. It starts to be in a shape that is not a huge stretch to make it fully no_std
even. Not the first priority tho but entirely possible.
After I have a similar state function for the receiver I’ll look at futures
crate, which claims to be no_std
compatible. I.e. idea would be to get yield once per iteration.
I’m not interested in async stuff for multi-threading. I’m interested single-threaded sequenced type of stuff, co-operative multitasking…
This should clarify how it is organized:
/// Map the previous frame type of the sender and incoming frame type of the
/// receiver to the next packet to be sent.
///
/// NOTE: ZRINIT is used here as a wait state, as the sender does not use it for
/// other purposes. Other than tat the states map to the packets that the sender
/// sends next.
const fn next_state(sender: Option<Type>, receiver: Type) -> Option<Type> {
match (sender, receiver) {
(None, Type::ZRINIT) => Some(Type::ZFILE),
(None, _) => Some(Type::ZRQINIT),
(Some(Type::ZRQINIT), Type::ZRINIT) => Some(Type::ZFILE),
(Some(Type::ZFILE), Type::ZRPOS) => Some(Type::ZDATA),
(Some(Type::ZFILE), Type::ZRINIT) => Some(Type::ZRINIT),
(Some(Type::ZRINIT), Type::ZRPOS) => Some(Type::ZDATA),
(Some(Type::ZDATA), Type::ZACK) => Some(Type::ZDATA),
(Some(Type::ZDATA), Type::ZRPOS) => Some(Type::ZDATA),
(Some(Type::ZDATA), Type::ZRINIT) => Some(Type::ZFIN),
(Some(Type::ZFIN), Type::ZFIN) => None,
(_, _) => None,
}
}
No reason to add complexity with fsmentry
. This is a nice clean const
function.
#zmodem2 is a nice history lesson to develop:
style: cleanup and fix cosmetic stuff
1. This inherits from original `zmodem` crate: "ZLDE" is in-fact ZDLE,
an acronym of "ZMODEM Data Link Escape" character.
2. Fine-tune use statements.
Link: https://wiki.synchro.net/ref:zmodem
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@iki.fi>
That link in the commit message is a great source of information on #zmodem.
After looking that for some time my state diagram for “sz” is simply this:
fsmentry::dsl! {
#[derive(Debug)]
pub Mode {
WaitingForReceiver -> Starting;
Starting -> Sending;
Starting -> WaitingForPosition;
Sending -> WaitingForPosition;
WaitingForPosition -> Sending;
Sending -> Ending;
}
}
This is scales exactly of the normal serial transfer. ZCHALLENGE/ZCOMPL, “corporate zmodem” (yes it was a real concept back in the say) and running commands by the request of the sender are definitely out of scope. So pretty clean and understandable in the end when you carve deep enough…
converted legacy hard coded test cases for frame to rstest
in the #zmodem 2 crate:
#[cfg(test)]
mod tests {
use crate::frame::*;
#[rstest::rstest]
#[case(Encoding::ZBIN, Type::ZRQINIT, &[ZPAD, ZLDE, Encoding::ZBIN as u8, 0, 0, 0, 0, 0, 0, 0])]
#[case(Encoding::ZBIN32, Type::ZRQINIT, &[ZPAD, ZLDE, Encoding::ZBIN32 as u8, 0, 0, 0, 0, 0, 29, 247, 34, 198])]
fn test_header(
#[case] encoding: Encoding,
#[case] frame_type: Type,
#[case] expected: &[u8]
) {
let header = Header::new(encoding, frame_type);
let mut packet = vec![];
new_frame(&header, &mut packet);
assert_eq!(packet, expected);
}
#[rstest::rstest]
#[case(Encoding::ZBIN, Type::ZRQINIT, &[1, 1, 1, 1], &[ZPAD, ZLDE, Encoding::ZBIN as u8, 0, 1, 1, 1, 1, 98, 148])]
#[case(Encoding::ZHEX, Type::ZRQINIT, &[1, 1, 1, 1], &[ZPAD, ZPAD, ZLDE, Encoding::ZHEX as u8, b'0', b'0', b'0', b'1', b'0', b'1', b'0', b'1', b'0', b'1', 54, 50, 57, 52, b'\r', b'\n', XON])]
fn test_header_with_flags(
#[case] encoding: Encoding,
#[case] frame_type: Type,
#[case] flags: &[u8; 4],
#[case] expected: &[u8]
) {
let header = Header::new(encoding, frame_type).flags(flags);
let mut packet = vec![];
new_frame(&header, &mut packet);
assert_eq!(packet, expected);
}
}
Should be easier to refactor the legacy code now as there is less raw code that might be affected in tests.
I hope I got this right (safety-proprty), i.e. so that references are enforced to have equal life-time:
impl<'a> From<&'a Header> for &'a [u8] {
fn from(value: &Header) -> Self {
// SAFETY: out-of-boundary is not possible, given that the size constraint
// exists in the struct definition. The lifetime parameter links the lifetime
// of the header reference to the slice.
unsafe { from_raw_parts((value as *const Header) as *const u8, size_of::<Header>()) }
}
}