use std::ops::{Deref, Index};
use std::ops::{DerefMut, IndexMut};
use std::slice::{self, SliceIndex};
#[repr(C)]
pub struct PyVec<T> {
ptr: *mut T,
len: u32,
}
impl<T> PyVec<T> {
pub fn as_ptr(&self) -> *const T {
self.ptr as *const T
}
pub fn as_mut_ptr(&mut self) -> *mut T {
self.ptr
}
pub fn len(&self) -> usize {
self.len as usize
}
pub fn to_vec(&self) -> Vec<T>
where
T: Clone,
{
(&**self).to_vec()
}
}
impl<T> Deref for PyVec<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
unsafe { slice::from_raw_parts(self.ptr, self.len as usize) }
}
}
impl<T> DerefMut for PyVec<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { slice::from_raw_parts_mut(self.ptr, self.len as usize) }
}
}
impl<T, I: SliceIndex<[T]>> Index<I> for PyVec<T> {
type Output = I::Output;
fn index(&self, index: I) -> &Self::Output {
Index::index(&**self, index)
}
}
impl<T, I: SliceIndex<[T]>> IndexMut<I> for PyVec<T> {
fn index_mut(&mut self, index: I) -> &mut Self::Output {
IndexMut::index_mut(&mut **self, index)
}
}
pub type PyStr = PyVec<u8>;
impl PyStr {
pub fn as_str(&self) -> Result<&str, std::str::Utf8Error> {
std::str::from_utf8(&*self)
}
pub unsafe fn as_str_unchecked(&self) -> &str {
std::str::from_utf8_unchecked(&*self)
}
}