Skip to content

Commit

Permalink
Add Copy + Clone to FFISlice, clarify some documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfbiedert committed Jun 29, 2024
1 parent 57ba53d commit 84979d7
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions core/src/patterns/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub struct FFISlice<'a, T> {
_phantom: PhantomData<&'a T>,
}

impl<'a, T> Copy for FFISlice<'a, T> {}

impl<'a, T> Clone for FFISlice<'a, T> {
fn clone(&self) -> Self {
*self
}
}

impl<'a, T> Default for FFISlice<'a, T> {
fn default() -> Self {
Self {
Expand All @@ -73,7 +81,9 @@ impl<'a, T> FFISlice<'a, T> {
}
}

/// Tries to return a slice if the pointer was not null.
/// Returns a safe Rust slice.
///
/// If the pointer was not null, the Rust slice will point to that data, otherwise an empty slice is returned.
pub fn as_slice(&self) -> &'a [T] {
if self.data.is_null() {
&[]
Expand Down Expand Up @@ -159,7 +169,9 @@ impl<'a, T> FFISliceMut<'a, T> {
}
}

/// Tries to return a slice if the pointer was not null.
/// Returns a safe, mutable Rust slice.
///
/// If the pointer was not null, the Rust slice will point to that data, otherwise an empty slice is returned.
pub fn as_slice_mut(&mut self) -> &'a mut [T] {
if self.data.is_null() {
&mut []
Expand All @@ -170,7 +182,9 @@ impl<'a, T> FFISliceMut<'a, T> {
}
}

/// Tries to return a slice if the pointer was not null.
/// Returns a safe Rust slice.
///
/// If the pointer was not null, the Rust slice will point to that data, otherwise an empty slice is returned.
pub fn as_slice(&self) -> &'a [T] {
if self.data.is_null() {
&[]
Expand Down

0 comments on commit 84979d7

Please sign in to comment.