From 06fcc5e9a802cfed9c34d9b2c1e82b685b645ef2 Mon Sep 17 00:00:00 2001 From: Mahmoud Mazouz Date: Thu, 14 Nov 2024 16:51:26 +0100 Subject: [PATCH] Accelerate `z_bytes_to_slice` --- src/collections.rs | 7 +++++++ src/zbytes.rs | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/collections.rs b/src/collections.rs index 5037e2b0b..04c69cbc4 100644 --- a/src/collections.rs +++ b/src/collections.rs @@ -254,6 +254,13 @@ impl From> for CSliceOwned { } } +impl From> for CSliceOwned { + fn from(value: Box<[u8]>) -> Self { + let slice = Box::leak(value); + CSliceOwned(CSlice::wrap(slice.as_ptr(), slice.len())) + } +} + impl From> for CSlice { fn from(value: Vec) -> Self { let slice: CSliceOwned = value.into(); diff --git a/src/zbytes.rs b/src/zbytes.rs index 5cc2eb874..94f4aaa51 100644 --- a/src/zbytes.rs +++ b/src/zbytes.rs @@ -136,7 +136,7 @@ pub unsafe extern "C" fn z_bytes_to_slice( ) -> z_result_t { let payload = this.as_rust_type_ref(); dst.as_rust_type_mut_uninit() - .write(payload.to_bytes().into_owned().into()); + .write(CSliceOwned::from(Box::<[u8]>::from(payload.to_bytes()))); result::Z_OK }