scsmithr commented on issue #7709: URL: https://github.com/apache/arrow-rs/issues/7709#issuecomment-2992989419
This is a perfect use case for a union. ```rust #[derive(Clone, Copy)] #[repr(C)] pub union BinaryView { inline: BinaryViewInline, reference: BinaryViewReference, } #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct BinaryViewInline { len: i32, inline: [u8; 12], } #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct BinaryViewReference { len: i32, prefix: [u8; 4], buffer_idx: i32, offset: i32, } impl BinaryView { pub fn new_inline(data: &[u8]) -> Self { let len = data.len(); assert!(len <= 12); let mut inline = BinaryViewInline { len: len as i32, inline: [0; 12], }; inline.inline[0..len].copy_from_slice(data); BinaryView { inline } } pub fn new_reference(data: &[u8], buffer_idx: i32, offset: i32) -> Self { let len = data.len(); assert!(len > 12); let mut reference = BinaryViewReference { len: len as i32, prefix: [0; 4], buffer_idx, offset, }; reference.prefix.copy_from_slice(&data[0..4]); BinaryView { reference } } pub const fn is_inline(&self) -> bool { // SAFETYE: i32 len is first field in both, safe to access from either // variant. unsafe { self.inline.len <= 12 } } pub const fn is_reference(&self) -> bool { unsafe { self.inline.len > 12 } } pub const fn data_len(&self) -> i32 { // SAFETY: `len` field is in the same place in both variants. unsafe { self.inline.len } } pub const fn as_inline(&self) -> &BinaryViewInline { assert!(self.is_inline()); unsafe { &self.inline } } pub const fn as_reference(&self) -> &BinaryViewReference { assert!(!self.is_inline()); unsafe { &self.reference } } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org