Skip to main content

CSliceMut

Struct CSliceMut 

Source
pub struct CSliceMut<'a, T> { /* private fields */ }
Expand description

The type representing an ‘unsafe’ mutable foreign chunk of memory.

§Example

use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let cslice = unsafe { CSliceMut::new(ptr, slice.len()) };

Implementations§

Source§

impl<'a, T> CSliceMut<'a, T>

Source

pub unsafe fn new(base: *mut T, len: usize) -> CSliceMut<'a, T>

Create a CSlice from a raw pointer to a buffer with a given length.

Panics if the given pointer is null. The returned slice will not attempt to deallocate the slice when dropped.

§Arguments
  • base - A raw pointer to a buffer
  • len - The number of elements in the buffer
§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
Source

pub fn get(&self, ofs: usize) -> Option<&T>

Retrieves an element at a given index, returning None if the requested index is greater than the length of the slice.

§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
assert_eq!(cslice.get(1), slice.get(1));
Source

pub unsafe fn get_unchecked(&self, ofs: usize) -> &T

Returns a reference to an element without doing any check.

§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
unsafe {
    assert_eq!(cslice.get_unchecked(1), slice.get_unchecked(1));
}
Source

pub fn get_mut(&mut self, ofs: usize) -> Option<&mut T>

Retrieves a mutable element at a given index, returning None if the requested index is greater than the length of the slice.

§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let mut cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
if let Some(el) = cslice.get_mut(1) {
    *el += 10;
}
assert_eq!(cslice[1], 11);
Source

pub unsafe fn get_unchecked_mut(&mut self, ofs: usize) -> &mut T

Returns a mutable reference to an element without doing any check.

§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let mut cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
unsafe { *cslice.get_unchecked_mut(1) += 10; }
assert_eq!(cslice[1], 11);
Source

pub fn len(&self) -> usize

Returns the number of items in this slice.

§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
assert_eq!(cslice.len(), slice.len());
Source

pub fn is_empty(&self) -> bool

Returns whether this slice is empty.

§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
assert_eq!(cslice.is_empty(), slice.is_empty());
Source

pub fn iter<'b>(&'b self) -> CSliceMutIter<'a, 'b, T>

Returns an iterator over CSliceMut.

§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
for elem in cslice.iter() {
    println!("=> {}", elem);
}
Source

pub fn iter_mut<'b>(&'b mut self) -> CSliceMutIterMut<'a, 'b, T>

Returns a mutable iterator over CSliceMut.

§Example
use c_vec::CSliceMut;

let slice = &mut [0, 1, 2];
let ptr = slice.as_mut_ptr();
let mut cslice = unsafe { CSliceMut::new(ptr, slice.len()) };
for elem in cslice.iter_mut() {
    *elem += 1;
}
assert_eq!(cslice[0], 1);

Trait Implementations§

Source§

impl<'a, T> AsMut<[T]> for CSliceMut<'a, T>

Source§

fn as_mut(&mut self) -> &mut [T]

View the stored data as a slice.

Source§

impl<'a, T> AsRef<[T]> for CSliceMut<'a, T>

Source§

fn as_ref(&self) -> &[T]

View the stored data as a slice.

Source§

impl<'a, T> Index<usize> for CSliceMut<'a, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T> IndexMut<usize> for CSliceMut<'a, T>

Source§

fn index_mut(&mut self, index: usize) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T: Clone> Into<Vec<T>> for CSliceMut<'a, T>

Source§

fn into(self: CSliceMut<'a, T>) -> Vec<T>

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

§

impl<'a, T> Freeze for CSliceMut<'a, T>

§

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

§

impl<'a, T> !Send for CSliceMut<'a, T>

§

impl<'a, T> !Sync for CSliceMut<'a, T>

§

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

§

impl<'a, T> UnsafeUnpin for CSliceMut<'a, T>

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.