From 5739b022cd9bbc4f09d2bab57fce6b81eb09d24b Mon Sep 17 00:00:00 2001 From: Albert Esteve Date: Wed, 15 Jul 2026 14:14:13 +0200 Subject: [PATCH] protocol: add as_bytes() impl to V4l2Event types Currently ErrorEvent derives IntoBytes, providing an as_bytes() implementation. However, DequeueBufferEvent and SessionEvent do not derive IntoBytes, which makes its usage asymmetric (e.g., in a match clause). Since deriving IntoBytes is not possible, implement the method directly in the structs. Signed-off-by: Albert Esteve --- device/src/protocol.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/device/src/protocol.rs b/device/src/protocol.rs index be8dafe..1d39c2f 100644 --- a/device/src/protocol.rs +++ b/device/src/protocol.rs @@ -291,6 +291,16 @@ impl DequeueBufferEvent { v4l2_buffer, } } + + pub fn as_bytes(&self) -> &[u8] { + // SAFETY: `DequeueBufferEvent` is a C-compatible struct with no padding and fully initialized bytes. + unsafe { + std::slice::from_raw_parts( + self as *const Self as *const u8, + std::mem::size_of::(), + ) + } + } } #[repr(C)] @@ -306,6 +316,16 @@ impl SessionEvent { v4l2_event, } } + + pub fn as_bytes(&self) -> &[u8] { + // SAFETY: `SessionEvent` is a C-compatible struct with no padding and fully initialized bytes. + unsafe { + std::slice::from_raw_parts( + self as *const Self as *const u8, + std::mem::size_of::(), + ) + } + } } #[allow(clippy::large_enum_variant)]