Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit

Permalink
Revert "fix(wrappers/java): experiment with using Vec for ByteArray"
Browse files Browse the repository at this point in the history
This reverts commit 2892a5c.
mate-from-mattr committed Dec 18, 2024
1 parent 6ba106d commit 0650dda
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -26,29 +26,39 @@ use std::{ptr, slice};
#[repr(C)]
pub struct ByteArray {
length: usize,
data: Vec<u8>,
data: *const u8,
}

impl Default for ByteArray {
fn default() -> Self {
Self {
length: 0,
data: Vec::new()
data: ptr::null(),
}
}
}

impl ByteArray {
/// Convert this into a byte vector
pub fn to_vec(&self) -> Vec<u8> {
self.data.clone()
if self.data.is_null() || self.length == 0 {
Vec::new()
} else {
unsafe { slice::from_raw_parts(self.data, self.length).to_vec() }
}
}

/// Convert this into a byte vector if possible
/// Some if success
/// None if not
pub fn to_opt_vec(&self) -> Option<Vec<u8>> {
Some(self.data.clone())
if self.data.is_null() {
None
} else if self.length == 0 {
Some(Vec::new())
} else {
Some(unsafe { slice::from_raw_parts(self.data, self.length).to_vec() })
}
}

///Convert to outgoing struct ByteBuffer
@@ -62,7 +72,7 @@ impl ByteArray {
let data = data.as_ref();
Self {
length: data.len(),
data: data.to_vec()
data: data.as_ptr() as *const u8,
}
}
}

0 comments on commit 0650dda

Please sign in to comment.