Can you use ident to play a type e.g.,
impl<'a> PartialEq<$ty> for TpmView<'a> {
fn eq(&self, other: &$ty) -> bool {
self.get() == *other
}
}
If “$ty” was ident, would this work?
Another more fun Rust related activity is figuring out how to nail batch file transfer test planning with emulated serial port that throttles the speed to a target BPS (in future also should have also signal noise emulation):
const ADJECTIVES: &[&str] = &["Quiet", "Loud", "Fast", "Slow", "Bright", "Dark"];
const NOUNS: &[&str] = &["One", "Two", "Three", "Four", "Five,", "Six"];
const EXTENSIONS: &[&str] = &["dat", "BIN", "log", "TMP", "txt"];
struct MockPort<R: Read, W: Write> {
r: R,
w: W,
bits_per_second: u32,
next_byte_due: Instant,
}
It generates 10 pseudorandom filenames and 100 KiB payloads.
Just solved a Rust sudoku for the next version of tpm2-protocol
I think this is quite clever trick to surpass some of the limitation of syntax tree macros:
macro_rules! tpm_integer {
($ty:ty) => {
pub mod $ty {
#[derive(Clone, Copy, Debug)]
pub struct TpmView<'a> {
pub slice: &'a [u8],
}
}
};
}
Further, TpmView
implements a trait called TpmView
. This circulates around limitation that type cannot be used to name things in syntax tree macros.
Otherwise, unless moving into procedural macros, I’d end up ugly declarations along the lines of:
tpm_integer!(u8, TpmViewU8, Unsigned);
tpm_integer!(i8, TpmViewI8, Signed);
tpm_integer!(u16, TpmViewU16, Unsigned);
tpm_integer!(i32, TpmViewI32, Signed);
tpm_integer!(u32, TpmViewU32, Unsigned);
tpm_integer!(u64, TpmViewU64, Unsigned);
Transparency has been the single biggest blocker in moving forward with zerocopy parser and this sort of “nails it”.
EDIT
I was wrong in that you could type as module name. However, the trick does still resolve the name coflict with the original type and view type. The minor inconvience of having to repeat it twice is totally acceptable vs having to manually name stuff:
tpm_integer!(u8, u8, Unsigned);
tpm_integer!(i8, i8, Signed);
tpm_integer!(u16, u16, Unsigned);
tpm_integer!(i32, i32, Signed);
tpm_integer!(u32, u32, Unsigned);
tpm_integer!(u64, u64, Unsigned);
This is absolutely sustainable for me :-) I.e. second parameter is name of the module.
Here’s a working proof of concept: https://git.kernel.org/pub/scm/linux/kernel/git/jarkko/tpm2-protocol.git/commit/?h=zerocopy&id=42f61d9d85e17f784f71c8ac64ee6adbb7171997
Zerocopy will be easy to implement now that I’ve found out how it is done by experimenting with basic types.
Memory requirements are relaxed as in:
70-80% of code base will be re-usable and remaining 20% is rewriting macros, tweaking a few call sites and creating a few new traits for macro consumption.
Despite 0.10.x having clone semantics, it is the correct traversal that is the hard problem here and it needs to be solved only once that data can be used to inject slice markers to exactly correct locations. This is why also parsing or building process nees no extra memory other than CPU cores registers.
The number lines in implementation will with higher odds go down rather than up. Looking pretty good…