1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
//! Traits for common operations on C++ vectors. use crate::{MutPtr, Ptr}; use std::slice; pub trait Data { type Output; /// Returns a pointer to the underlying memory buffer. /// /// # Safety /// /// The caller must make sure `self` contains a valid pointer. This function /// may invoke arbitrary foreign code, so no safety guarantees can be made. unsafe fn data(&self) -> Self::Output; } pub trait DataMut { type Output; /// Returns a pointer to the underlying memory buffer. /// /// # Safety /// /// The caller must make sure `self` contains a valid pointer. This function /// may invoke arbitrary foreign code, so no safety guarantees can be made. unsafe fn data_mut(&mut self) -> Self::Output; } pub trait Size { /// Returns size of the underlying buffer in elements. /// /// # Safety /// /// The caller must make sure `self` contains a valid pointer. This function /// may invoke arbitrary foreign code, so no safety guarantees can be made. unsafe fn size(&self) -> usize; } pub trait VectorAsSlice { type Item; /// Returns the content of the vector as a slice. /// /// # Safety /// /// The caller must make sure `self` contains a valid pointer. The content must /// not be read or modified through other ways while the returned slice exists. /// This function /// may invoke arbitrary foreign code, so no safety guarantees can be made. unsafe fn vector_as_slice(&self) -> &[Self::Item]; } pub trait VectorAsMutSlice { type Item; /// Returns the content of the vector as a slice. /// /// # Safety /// /// The caller must make sure `self` contains a valid pointer. The content must /// not be read or modified through other ways while the returned slice exists. /// This function /// may invoke arbitrary foreign code, so no safety guarantees can be made. unsafe fn vector_as_mut_slice(&mut self) -> &mut [Self::Item]; } impl<V, T> VectorAsSlice for V where V: Data<Output = Ptr<T>> + Size, { type Item = T; unsafe fn vector_as_slice(&self) -> &[T] { let ptr = self.data().as_raw_ptr(); let size = self.size(); slice::from_raw_parts(ptr, size) } } impl<V, T> VectorAsMutSlice for V where V: DataMut<Output = MutPtr<T>> + Size, { type Item = T; unsafe fn vector_as_mut_slice(&mut self) -> &mut [T] { let ptr = self.data_mut().as_mut_raw_ptr(); let size = self.size(); slice::from_raw_parts_mut(ptr, size) } }