Struct uniffi::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
impl ForeignBytes
pub unsafe fn from_raw_parts(data: *const u8, len: i32) -> 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] ⓘ
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.