[][src]Struct abi_stable::std_types::RSlice

#[repr(C)]pub struct RSlice<'a, T> { /* fields omitted */ }

Ffi-safe equivalent of &'a [T]

As of the writing this documentation the abi stability of &[T] is not yet guaranteed.

Lifetime problems

Because RSlice dereferences into a slice,you can call slice methods on it.

If you call a slice method that returns a borrow into the slice, it will have the lifetime of the let slice: RSlice<'a,[T]> variable instead of the 'a lifetime that it's parameterized over.

To get a slice with the same lifetime as an RSlice, one must use the RSlice::as_slice method.

Example of what would not work:

This example deliberately fails to compile
use abi_stable::std_types::RSlice;

fn into_slice<'a,T>(slic:RSlice<'a,T>)->&'a [T] {
    &*slic
}

Example of what would work:

use abi_stable::std_types::RSlice;

fn into_slice<'a,T>(slic:RSlice<'a,T>)->&'a [T] {
    slic.as_slice()
}

Example

Defining an extern fn that returns a reference to the first element that compares equal to a parameter.

use abi_stable::{
    std_types::RSlice,
    sabi_extern_fn,
};

#[sabi_extern_fn]
pub fn find_first_mut<'a,T>(slice_:RSlice<'a,T>,element:&T)->Option<&'a T>
where
    T:std::cmp::PartialEq
{
    slice_.iter()
        .position(|x| x==element )
        .map(|i| &slice_.as_slice()[i] )
}

Implementations

impl<'a, T: 'a> RSlice<'a, T>[src]

pub const EMPTY: Self[src]

An empty slice.

pub const unsafe fn from_raw_parts(ptr_: *const T, len: usize) -> Self[src]

Constructs an RSlice<'a,T> from a pointer to the first element, and a length.

Safety

Callers must ensure that:

  • ptr_ points to valid memory,

  • ptr_ .. ptr+len range is accessible memory.

  • ptr_ is aligned to T.

  • The data ptr_ points to must be valid for the 'a lifetime.

Examples

This function unsafely converts a &[T] to an RSlice<T>, equivalent to doing RSlice::from_slice.

use abi_stable::std_types::RSlice;

fn convert<T>(slice_:&[T])->RSlice<'_,T>{
    unsafe{
        RSlice::from_raw_parts( slice_.as_ptr(), slice_.len() )
    }
}

impl<'a, T> RSlice<'a, T>[src]

pub fn as_slice(&self) -> &'a [T][src]

Creates an &'a [T] with access to all the elements of this slice.

Example

use abi_stable::std_types::RSlice;

assert_eq!(RSlice::from_slice(&[0,1,2,3]).as_slice(), &[0,1,2,3]);

pub const fn as_ptr(&self) -> *const T[src]

Gets a raw pointer to the start of the slice.

pub const fn len(&self) -> usize[src]

The length (in elements) of this slice.

Example

use abi_stable::std_types::RSlice;

assert_eq!(RSlice::<u8>::from_slice(&[]).len(), 0);
assert_eq!(RSlice::from_slice(&[0]).len(), 1);
assert_eq!(RSlice::from_slice(&[0,1]).len(), 2);

pub const fn is_empty(&self) -> bool[src]

Whether this slice is empty.

Example

use abi_stable::std_types::RSlice;

assert_eq!(RSlice::<u8>::from_slice(&[]).is_empty(), true);
assert_eq!(RSlice::from_slice(&[0]).is_empty(), false);
assert_eq!(RSlice::from_slice(&[0,1]).is_empty(), false);

impl<'a, T> RSlice<'a, T>[src]

pub const fn empty() -> Self[src]

Creates an empty slice

pub const fn from_ref(ref_: &'a T) -> Self[src]

Converts a reference to T to a single element RSlice<'a,T>.

Note:this function does not copy anything.

Example

use abi_stable::std_types::RSlice;

assert_eq!(RSlice::from_ref(&0), RSlice::from_slice(&[0]) );
assert_eq!(RSlice::from_ref(&1), RSlice::from_slice(&[1]) );
assert_eq!(RSlice::from_ref(&2), RSlice::from_slice(&[2]) );
 

pub const fn from_slice(slic: &'a [T]) -> Self[src]

Converts a &[T] to an RSlice<'_,T>.

Example

use abi_stable::std_types::RSlice;
 
let empty:&[u8]=&[];
 
assert_eq!(RSlice::<u8>::from_slice(&[]).as_slice(), empty);
assert_eq!(RSlice::from_slice(&[0]).as_slice()     , &[0][..]);
assert_eq!(RSlice::from_slice(&[0,1]).as_slice()   , &[0,1][..]);
 

pub fn slice<I>(&self, i: I) -> RSlice<'a, T>

Notable traits for RSlice<'a, u8>

impl<'a> Read for RSlice<'a, u8>
where
    [T]: Index<I, Output = [T]>, 
[src]

Creates an RSlice<'a,T> with access to the range range of elements.

This is an inherent method instead of an implementation of the std::ops::Index trait because it does not return a reference.

Example

use abi_stable::std_types::RSlice;

let slic=RSlice::from_slice(&[0,1,2,3]);

assert_eq!(slic.slice(..),RSlice::from_slice(&[0,1,2,3]));
assert_eq!(slic.slice(..2),RSlice::from_slice(&[0,1]));
assert_eq!(slic.slice(2..),RSlice::from_slice(&[2,3]));
assert_eq!(slic.slice(1..3),RSlice::from_slice(&[1,2]));

pub fn to_rvec(&self) -> RVec<T>

Notable traits for RVec<u8>

impl Write for RVec<u8>
where
    T: Clone
[src]

Creates a new RVec<T> and clones all the elements of this slice into it.

Example

use abi_stable::std_types::{RSlice,RVec};

let slic=RSlice::from_slice(&[0,1,2,3]);

assert_eq!( slic.slice(..).to_rvec(), RVec::from_slice(&[0,1,2,3]) );
assert_eq!( slic.slice(..2).to_rvec(), RVec::from_slice(&[0,1]) );
assert_eq!( slic.slice(2..).to_rvec(), RVec::from_slice(&[2,3]) );
assert_eq!( slic.slice(1..3).to_rvec(), RVec::from_slice(&[1,2]) );

pub const unsafe fn transmute_ref<U>(self) -> RSlice<'a, U>

Notable traits for RSlice<'a, u8>

impl<'a> Read for RSlice<'a, u8>
where
    U: 'a, 
[src]

Transmutes n RSlice<'a,T> to a RSlice<'a,U>

Safety

This has the same safety requirements as calling std::mem::transmute to transmute a &'a [T] to a &'a [U].

Trait Implementations

impl<'a, T: 'a> AsRef<[T]> for RSlice<'a, T>[src]

impl<'a, T: 'a> Borrow<[T]> for RSlice<'a, T>[src]

impl<'a> BufRead for RSlice<'a, u8>[src]

impl<'a, T> Clone for RSlice<'a, T>[src]

impl<'a, T> Copy for RSlice<'a, T>[src]

impl<'a, T> Debug for RSlice<'a, T> where
    T: Debug
[src]

impl<'a, T> Default for RSlice<'a, T>[src]

impl<'a, T: 'a> Deref for RSlice<'a, T>[src]

type Target = [T]

The resulting type after dereferencing.

impl<'de, T> Deserialize<'de> for RSlice<'de, T> where
    &'de [T]: Deserialize<'de>, 
[src]

impl<'a, T> Eq for RSlice<'a, T> where
    T: Eq
[src]

impl<'a, T> From<&'a [T]> for RSlice<'a, T>[src]

impl<'a, T> From<RSlice<'a, T>> for RCow<'a, [T]> where
    T: Clone
[src]

impl<'a, T> GetStaticEquivalent_ for RSlice<'a, T> where
    T: __StableAbi,
    T: 'a, 
[src]

type StaticEquivalent = _static_RSlice<'static, __GetStaticEquivalent<T>>

impl<'a, T> Hash for RSlice<'a, T> where
    T: Hash
[src]

impl<'a, T> Into<&'a [T]> for RSlice<'a, T>[src]

impl<'a, T> IntoIterator for RSlice<'a, T>[src]

type Item = &'a T

The type of the elements being iterated over.

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?

impl<'a, T> IntoReprRust for RSlice<'a, T>[src]

type ReprRust = &'a [T]

The #[repr(Rust)] equivalent.

impl<'a, T> Ord for RSlice<'a, T> where
    T: Ord
[src]

impl<'a, T> PartialEq<RSlice<'a, T>> for RSlice<'a, T> where
    T: PartialEq
[src]

impl<'a, T> PartialOrd<RSlice<'a, T>> for RSlice<'a, T> where
    T: PartialOrd
[src]

impl<'a> Read for RSlice<'a, u8>[src]

impl<'a, T> Send for RSlice<'a, T> where
    &'a [T]: Send
[src]

impl<'a, T> Serialize for RSlice<'a, T> where
    T: Serialize
[src]

impl<'a, T> StableAbi for RSlice<'a, T> where
    T: __StableAbi,
    T: 'a, 
[src]

type IsNonZeroType = False

Whether this type has a single invalid bit-pattern. Read more

impl<'a, T> Sync for RSlice<'a, T> where
    &'a [T]: Sync
[src]

Auto Trait Implementations

impl<'a, T> RefUnwindSafe for RSlice<'a, T> where
    T: RefUnwindSafe

impl<'a, T> Unpin for RSlice<'a, T>

impl<'a, T> UnwindSafe for RSlice<'a, T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<'a, T> BorrowOwned<'a> for T where
    T: 'a + Clone
[src]

type ROwned = T

The owned type, stored in RCow::Owned

type RBorrowed = &'a T

The borrowed type, stored in RCow::Borrowed

impl<T> From<T> for T[src]

impl<T> GetWithMetadata for T[src]

type ForSelf = WithMetadata_<T, T>

This is always WithMetadata_<Self, Self>

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The error type returned when the conversion fails.

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more

impl<This> ValidTag_Bounds for This where
    This: Debug + Clone + PartialEq<This>, 
[src]