use std::{slice};
use core::hash::Hash;
use std::mem::{forget, size_of, transmute};
use serde::{Serialize};
use super::unicode_string_helpers::{*};
pub trait OwnedKey : 'static + Sized + Serialize + serde::de::DeserializeOwned + Key {
fn as_string(&self) -> Option<String>;
fn borrow_str(&self) -> Option<&str>;
fn as_vec(&self) -> Option<Vec<Self::KeyCharT>>;
fn into_vec(self) -> Vec<Self::KeyCharT>;
fn move_into_buf<'a>(&'a self, buf : &'a mut Vec<Self::KeyCharT>) -> &'a Vec<Self::KeyCharT>; fn borrow_vec(&self) -> Option<&[Self::KeyCharT]>;
fn from_key<K : Key + KeyUnsafe<KeyCharT = Self::KeyCharT>>(k : &K) -> Self; fn from_string(s : String) -> Self;
fn from_vec(v : Vec<Self::KeyCharT>) -> Self;
}
impl OwnedKey for String {
#[inline(always)]
fn as_string(&self) -> Option<String> {
Some(self.clone())
}
#[inline(always)]
fn borrow_str(&self) -> Option<&str> {
Some(self)
}
#[inline(always)]
fn as_vec(&self) -> Option<Vec<char>> {
Some(self.chars().collect())
}
#[inline(always)]
fn into_vec(self) -> Vec<char> {
let num_chars = self.num_chars();
let mut result_vec = Vec::with_capacity(num_chars);
for the_char in self.chars() {
result_vec.push(the_char);
}
result_vec
}
#[inline(always)]
fn move_into_buf<'a>(&'a self, buf : &'a mut Vec<char>) -> &'a Vec<char> {
let mut num_chars = 0;
for (i, the_char) in self.chars().enumerate() {
let element = unsafe{ buf.get_unchecked_mut(i) };
*element = the_char;
num_chars = i;
}
unsafe{ buf.set_len(num_chars+1) };
buf
}
#[inline(always)]
fn borrow_vec(&self) -> Option<&[char]> {
None
}
#[inline(always)]
fn from_key<K : Key>(k : &K) -> Self {
k.get_key_string()
}
#[inline(always)]
fn from_string(s : String) -> Self {
s
}
#[inline(always)]
fn from_vec(v : Vec<char>) -> Self {
v.into_iter().collect()
}
}
impl <KeyCharT : 'static + Copy + Eq + Hash + Serialize + serde::de::DeserializeOwned>OwnedKey for Vec<KeyCharT>
{
#[inline(always)]
fn as_string(&self) -> Option<String> {
None
}
#[inline(always)]
fn borrow_str(&self) -> Option<&str> {
None
}
#[inline(always)]
fn as_vec(&self) -> Option<Vec<KeyCharT>> {
Some(self.clone())
}
#[inline(always)]
fn into_vec(self) -> Vec<KeyCharT> {
self
}
#[inline(always)]
fn move_into_buf<'a>(&'a self, _buf : &'a mut Vec<KeyCharT>) -> &'a Vec<KeyCharT> {
self
}
#[inline(always)]
fn borrow_vec(&self) -> Option<&[KeyCharT]> {
Some(&self[..])
}
#[inline(always)]
fn from_key<K : Key + KeyUnsafe<KeyCharT = KeyCharT>>(k : &K) -> Self { k.get_key_chars()
}
#[inline(always)]
fn from_string(_s : String) -> Self {
panic!() }
#[inline(always)]
fn from_vec(v : Vec<KeyCharT>) -> Self {
v
}
}
pub trait IntoKey {
type Key: Key;
fn into_key(self) -> Self::Key;
}
impl<K> IntoKey for K
where
K : Key
{
type Key = Self;
#[inline(always)]
fn into_key(self) -> Self::Key {
self
}
}
impl<'a> IntoKey for &'a String {
type Key = &'a str;
#[inline(always)]
fn into_key(self) -> Self::Key {
self as &str
}
}
impl<'a, KeyCharT : 'static + Copy + Eq + Hash + Serialize + serde::de::DeserializeOwned, const SIZE: usize> IntoKey for &'a [KeyCharT; SIZE] {
type Key = &'a [KeyCharT];
#[inline(always)]
fn into_key(self) -> Self::Key {
&self[..]
}
}
pub trait Key : Eq + Hash + Clone + KeyUnsafe {
fn num_chars(&self) -> usize;
fn as_bytes(&self) -> &[u8];
fn into_bytes(self) -> Vec<u8>;
fn borrow_key_chars(&self) -> Option<&[Self::KeyCharT]>;
fn get_key_chars(&self) -> Vec<Self::KeyCharT>;
fn borrow_key_str(&self) -> Option<&str>;
fn get_key_string(&self) -> String;
}
pub trait KeyUnsafe : Eq + Hash + Clone {
type KeyCharT : Clone;
unsafe fn from_owned_unsafe<OwnedKeyT : OwnedKey + KeyUnsafe<KeyCharT = Self::KeyCharT>>(owned_key : &OwnedKeyT) -> Self;
}
impl <KeyCharT>Key for &[KeyCharT]
where
KeyCharT : 'static + Copy + Eq + Hash + Serialize + serde::de::DeserializeOwned,
{
#[inline(always)]
fn num_chars(&self) -> usize {
self.len()
}
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
let len = self.len();
unsafe { slice::from_raw_parts(self.as_ptr() as *const u8, size_of::<KeyCharT>() * len) }
}
#[inline(always)]
fn into_bytes(self) -> Vec<u8> {
let mut owned_vec = self.to_vec();
let len = owned_vec.len();
let cap = owned_vec.capacity();
let result = unsafe { Vec::<u8>::from_raw_parts(owned_vec.as_mut_ptr() as *mut u8, size_of::<KeyCharT>() * len, size_of::<KeyCharT>() * cap) };
forget(owned_vec); result
}
#[inline(always)]
fn borrow_key_chars(&self) -> Option<&[KeyCharT]> {
Some(self)
}
#[inline(always)]
fn get_key_chars(&self) -> Vec<KeyCharT> {
self.to_vec()
}
#[inline(always)]
fn borrow_key_str(&self) -> Option<&str> {
None
}
#[inline(always)]
fn get_key_string(&self) -> String {
if size_of::<KeyCharT>() == size_of::<char>() {
self.iter().map(|key_char| unsafe{ transmute::<&KeyCharT, &char>(key_char) }).collect()
} else {
panic!();
}
}
}
impl <'a, KeyCharT>KeyUnsafe for &'a [KeyCharT]
where
KeyCharT : 'static + Copy + Eq + Hash + Serialize + serde::de::DeserializeOwned
{
type KeyCharT = KeyCharT;
unsafe fn from_owned_unsafe<OwnedKeyT : OwnedKey + KeyUnsafe<KeyCharT = KeyCharT>>(owned_key : &OwnedKeyT) -> Self {
let result = owned_key.borrow_vec().unwrap();
transmute::<&[KeyCharT], &'a [KeyCharT]>(result)
}
}
impl <KeyCharT>Key for Vec<KeyCharT>
where
KeyCharT : 'static + Copy + Eq + Hash + Serialize + serde::de::DeserializeOwned,
{
#[inline(always)]
fn num_chars(&self) -> usize {
self.len()
}
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
let len = self.len();
unsafe { slice::from_raw_parts(self.as_ptr() as *const u8, size_of::<KeyCharT>() * len) }
}
#[inline(always)]
fn into_bytes(self) -> Vec<u8> {
let mut mut_self = self;
let len = mut_self.len();
let cap = mut_self.capacity();
let result = unsafe { Vec::<u8>::from_raw_parts(mut_self.as_mut_ptr() as *mut u8, size_of::<KeyCharT>() * len, size_of::<KeyCharT>() * cap) };
forget(mut_self);
result
}
#[inline(always)]
fn borrow_key_chars(&self) -> Option<&[KeyCharT]> {
Some(&self[..])
}
#[inline(always)]
fn get_key_chars(&self) -> Vec<KeyCharT> {
self.clone()
}
#[inline(always)]
fn borrow_key_str(&self) -> Option<&str> {
None
}
#[inline(always)]
fn get_key_string(&self) -> String {
if size_of::<KeyCharT>() == size_of::<char>() {
self.iter().map(|key_char| unsafe{ transmute::<&KeyCharT, &char>(key_char) }).collect()
} else {
panic!();
}
}
}
impl <KeyCharT>KeyUnsafe for Vec<KeyCharT>
where
KeyCharT : 'static + Copy + Eq + Hash + Serialize + serde::de::DeserializeOwned,
{
type KeyCharT = KeyCharT;
unsafe fn from_owned_unsafe<OwnedKeyT : OwnedKey + KeyUnsafe<KeyCharT = KeyCharT>>(owned_key : &OwnedKeyT) -> Self {
owned_key.as_vec().unwrap()
}
}
impl Key for &str
{
#[inline(always)]
fn num_chars(&self) -> usize {
unicode_len(self)
}
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
str::as_bytes(self)
}
#[inline(always)]
fn into_bytes(self) -> Vec<u8> {
let bytes_slice = str::as_bytes(self);
bytes_slice.to_vec()
}
#[inline(always)]
fn borrow_key_chars(&self) -> Option<&[char]> {
None
}
#[inline(always)]
fn get_key_chars(&self) -> Vec<char> {
self.chars().collect()
}
#[inline(always)]
fn borrow_key_str(&self) -> Option<&str> {
Some(self)
}
#[inline(always)]
fn get_key_string(&self) -> String {
self.to_string()
}
}
impl <'a>KeyUnsafe for &'a str {
type KeyCharT = char;
unsafe fn from_owned_unsafe<OwnedKeyT : OwnedKey>(owned_key : &OwnedKeyT) -> Self {
let result = owned_key.borrow_str().unwrap();
transmute::<&str, &'a str>(result)
}
}
impl Key for String
{
#[inline(always)]
fn num_chars(&self) -> usize {
unicode_len(self)
}
#[inline(always)]
fn as_bytes(&self) -> &[u8] {
self.as_bytes()
}
#[inline(always)]
fn into_bytes(self) -> Vec<u8> {
self.into_bytes()
}
#[inline(always)]
fn borrow_key_chars(&self) -> Option<&[char]> {
None
}
#[inline(always)]
fn get_key_chars(&self) -> Vec<char> {
self.chars().collect()
}
#[inline(always)]
fn borrow_key_str(&self) -> Option<&str> {
Some(self)
}
#[inline(always)]
fn get_key_string(&self) -> String {
self.clone()
}
}
impl KeyUnsafe for String {
type KeyCharT = char;
unsafe fn from_owned_unsafe<OwnedKeyT : OwnedKey>(owned_key : &OwnedKeyT) -> Self {
owned_key.as_string().unwrap()
}
}