From 129a688136d9e32584db3879559215eeb33fbe27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Rouill=C3=A9?= Date: Fri, 28 Feb 2020 18:39:46 +0100 Subject: [PATCH] limit risks of transmute errors of fdb_sys::FDBKeyValue to FdbKeyValue --- foundationdb/src/future.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/foundationdb/src/future.rs b/foundationdb/src/future.rs index 7a4b2696..6d94a473 100644 --- a/foundationdb/src/future.rs +++ b/foundationdb/src/future.rs @@ -434,30 +434,24 @@ impl fmt::Debug for FdbValue { /// A keyvalue owned by a foundationDB future /// -/// # Internal info: -/// -/// Uses repr(C, packed(4)) because c API uses 4-byte alignment for this struct -/// /// Because the data it represent is owned by the future in FdbValues, you /// can never own a FdbKeyValue directly, you can only have references to it. /// This way, you can never obtain a lifetime greater than the lifetime of the /// slice that gave you access to it. -#[repr(C, packed(4))] -pub struct FdbKeyValue { - key: *const u8, - key_len: i32, - value: *const u8, - value_len: i32, -} +#[repr(transparent)] +pub struct FdbKeyValue(fdb_sys::FDBKeyValue); + impl FdbKeyValue { /// key pub fn key(&self) -> &[u8] { - unsafe { std::slice::from_raw_parts(self.key, self.key_len as usize) } + unsafe { std::slice::from_raw_parts(self.0.key as *const u8, self.0.key_length as usize) } } /// value pub fn value(&self) -> &[u8] { - unsafe { std::slice::from_raw_parts(self.value, self.value_len as usize) } + unsafe { + std::slice::from_raw_parts(self.0.value as *const u8, self.0.value_length as usize) + } } }