Struct uniffi::ffi::ForeignBytes

#[repr(C)]
pub struct ForeignBytes { len: i32, data: *const u8, }
Expand description

Support for reading a slice of foreign-language-allocated bytes over the FFI.

Foreign language code can pass a slice of bytes by providing a data pointer and length, and this struct provides a convenient wrapper for working with that pair. Naturally, this can be tremendously unsafe! So here are the details:

  • The foreign language code must ensure the provided buffer stays alive and unchanged for the duration of the call to which the ForeignBytes struct was provided.

To work with the bytes in Rust code, use as_slice() to view the data as a &[u8].

Implementation note: all the fields of this struct are private and it has no constructors, so consuming crates cant create instances of it. If you’ve got a ForeignBytes, then you received it over the FFI and are assuming that the foreign language code is upholding the above invariants.

This struct is based on ByteBuffer from the ffi-support crate, but modified to give a read-only view of externally-provided bytes.

Fields§

§len: i32§data: *const u8

Implementations§

§

impl ForeignBytes

pub unsafe fn from_raw_parts(data: *const u8, len: i32) -> ForeignBytes

Creates a ForeignBytes from its constituent fields.

This is intended mainly as an internal convenience function and should not be used outside of this module.

Safety

You must ensure that the raw parts uphold the documented invariants of this class.

pub fn as_slice(&self) -> &[u8]

View the foreign bytes as a &[u8].

Panics

Panics if the provided struct has a null pointer but non-zero length. Panics if the provided length is negative.

pub fn len(&self) -> usize

Get the length of this slice of bytes.

Panics

Panics if the provided length is negative.

pub fn is_empty(&self) -> bool

Returns true if the length of this slice of bytes is 0.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.