Skip to content

Commit

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

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

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

/// Convert this into a byte vector if possible
/// Some if success
/// None if not
pub fn to_opt_vec(&self) -> Option<Vec<u8>> {
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() })
}
Some(self.data.clone())
}

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

0 comments on commit 2892a5c

Please sign in to comment.