use std::{fmt::Display, ops::Deref, vec::Vec as StdVec};
#[repr(C)]
#[derive(Debug)]
pub struct Vec<T> {
ptr: *mut T,
len: usize,
}
impl<T> Deref for Vec<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
}
}
impl<T> Drop for Vec<T> {
fn drop(&mut self) {
unsafe {
let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut(self.ptr, self.len));
};
}
}
impl<T> From<StdVec<T>> for Vec<T> {
fn from(vec: StdVec<T>) -> Self {
let vec = vec.into_boxed_slice();
let len = vec.len();
let ptr = Box::into_raw(vec).cast::<T>();
Self { ptr, len }
}
}
impl<T> PartialEq for Vec<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.len == other.len && self.as_ref() == other.as_ref()
}
}
#[repr(C)]
#[derive(Debug)]
pub struct Str {
ptr: *const u8,
len: usize,
}
impl Str {
#[must_use]
pub fn str(&self) -> &'static str {
unsafe { std::str::from_utf8_unchecked(std::slice::from_raw_parts(self.ptr, self.len)) }
}
}
impl From<&'static str> for Str {
fn from(val: &'static str) -> Self {
let ptr = val.as_ptr();
let len = val.len();
Self { ptr, len }
}
}
impl AsRef<str> for Str {
fn as_ref(&self) -> &str {
self.str()
}
}
impl Display for Str {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.str().fmt(f)
}
}
impl PartialEq for Str {
fn eq(&self, other: &Self) -> bool {
self.len == other.len && self.str() == other.str()
}
}
#[repr(C)]
#[derive(Debug, PartialEq)]
pub struct RString {
inner: Vec<u8>,
}
impl RString {
#[inline]
#[must_use]
pub fn as_str(&self) -> &str {
std::str::from_utf8(&self.inner).expect("RString value is not valid UTF-8")
}
}
impl From<&str> for RString {
fn from(s: &str) -> Self {
Self {
inner: s.as_bytes().to_vec().into(),
}
}
}
impl From<String> for RString {
fn from(s: String) -> Self {
Self {
inner: s.into_bytes().into(),
}
}
}
impl AsRef<str> for RString {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Display for RString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[repr(C, u8)]
#[derive(Debug)]
pub enum Option<T> {
Some(T),
None,
}
impl<T> From<std::option::Option<T>> for Option<T> {
fn from(opt: std::option::Option<T>) -> Self {
match opt {
Some(val) => Self::Some(val),
None => Self::None,
}
}
}
impl<T> PartialEq for Option<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Some(a), Self::Some(b)) => a == b,
(Self::None, Self::None) => true,
_ => false,
}
}
}