소스 검색

Adds many Vec methods

There are some that will be prefixed with vec for if they match the same
name under hashmap but have different requirements (such as insert
taking a key not a index, etc.)
Steve Thielemann 2 달 전
부모
커밋
36f6727712
1개의 변경된 파일181개의 추가작업 그리고 2개의 파일을 삭제
  1. 181 2
      src/data.rs

+ 181 - 2
src/data.rs

@@ -1,4 +1,4 @@
-use std::{collections::HashMap, fmt::Display};
+use std::{collections::HashMap, fmt::Display, slice::{SliceIndex, Iter, IterMut}};
 
 use crate::Shared;
 
@@ -456,7 +456,7 @@ impl TryInto<bool> for Data {
     type Error = ();
     fn try_into(self) -> Result<bool, Self::Error> {
         match self {
-            Self::String(s) => Ok(s.is_empty()),
+            Self::String(s) => Ok(!s.is_empty()),
             Self::I8(v) => Ok(v != 0),
             Self::I16(v) => Ok(v != 0),
             Self::I32(v) => Ok(v != 0),
@@ -506,6 +506,22 @@ impl From<Data> for Shared<Data> {
 }
 
 impl Data {
+    /// Constructs a new empty Vec of Vec<Data>
+    pub fn new_vec() -> Self {
+        Self::Vec(Vec::new())
+    }
+    /// Constructs a new empty Map of HashMap<String, Data>
+    pub fn new_map() -> Self {
+        Self::Map(HashMap::new())
+    }
+    /// Constructs a new Vec with capacity
+    pub fn vec_with_capacity(cap: usize) -> Self {
+        Self::Vec(Vec::with_capacity(cap))
+    }
+    /// Constructs a new Map with capacity
+    pub fn map_with_capacity(cap: usize) -> Self {
+        Self::Map(HashMap::with_capacity(cap))
+    }
     /// Is Data not the variant None
     /// 
     /// This is useful for using Data similar to a Option
@@ -548,6 +564,169 @@ impl Data {
     pub fn is_nested(&self) -> bool {
         matches!(self, Self::Vec(_) | Self::Map(_))
     }
+    // socks, blue, bed
+    pub fn is_vec(&self) -> bool {
+        matches!(self, Self::Vec(_))
+    }
+    pub fn is_map(&self) -> bool {
+        matches!(self, Self::Map(_))
+    }
+    pub fn push(&mut self, v: Data) {
+        match self {
+            Self::Vec(vec) => {
+                vec.push(v);
+            }
+            _ => (),
+        }
+    }
+    pub fn insert(&mut self, k: String, v: Data) {
+        match self {
+            Self::Map(hm) => {
+                hm.insert(k, v);
+            }
+            _ => (),
+        }
+    }
+    pub fn len(&self) -> usize {
+        match self {
+            Self::Vec(v) => v.len(),
+            Self::Map(m) => m.len(),
+            _ => 0,
+        }
+    }
+    pub fn capacity(&self) -> usize {
+        match self {
+            Self::Vec(v) => v.capacity(),
+            Self::Map(m) => m.capacity(),
+            _ => 0,
+        }
+    }
+    pub fn reserve(&mut self, additional: usize) {
+        match self {
+            Self::Vec(v) => v.reserve(additional),
+            Self::Map(m) => m.reserve(additional),
+            _ => (),
+        }
+    }
+    pub fn shrink_to_fit(&mut self) {
+        match self {
+            Self::Vec(v) => v.shrink_to_fit(),
+            Self::Map(m) => m.shrink_to_fit(),
+            _ => (),
+        }
+    }
+    pub fn shrink_to(&mut self, min: usize) {
+        match self {
+            Self::Vec(v) => v.shrink_to(min),
+            Self::Map(m) => m.shrink_to(min),
+            _ => (),
+        }
+    }
+    pub fn truncate(&mut self, len: usize) {
+        match self {
+            Self::Vec(v) => v.truncate(len),
+            _ => (),
+        }
+    }
+    pub fn remove(&mut self, index: usize) -> Data {
+        match self {
+            Self::Vec(v) => v.remove(index),
+            _ => Data::None,
+        }
+    }
+    pub fn pop(&mut self) -> Option<Data> {
+        match self {
+            Self::Vec(v) => v.pop(),
+            _ => None,
+        }
+    }
+    pub fn clear(&mut self) {
+        match self {
+            Self::Vec(v) => v.clear(),
+            Self::Map(m) => m.clear(),
+            _ => (),
+        }
+    }
+    pub fn is_empty(&self) -> bool {
+        match self {
+            Self::String(s) => s.is_empty(),
+            Self::Vec(v) => v.is_empty(),
+            Self::Map(m) => m.is_empty(),
+            _ => false,
+        }
+    }
+    pub fn resize(&mut self, len: usize, value: Data) {
+        match self {
+            Self::Vec(v) => v.resize(len, value),
+            _ => (),
+        }
+    }
+    pub fn first(&self) -> Option<&Data> {
+        match self {
+            Self::Vec(v) => v.first(),
+            _ => None,
+        }
+    }
+    pub fn first_mut(&mut self) -> Option<&mut Data> {
+        match self {
+            Self::Vec(v) => v.first_mut(),
+            _ => None,
+        }
+    }
+    pub fn last(&self) -> Option<&Data> {
+        match self {
+            Self::Vec(v) => v.last(),
+            _ => None,
+        }
+    }
+    pub fn last_mut(&mut self) -> Option<&mut Data> {
+        match self {
+            Self::Vec(v) => v.last_mut(),
+            _ => None,
+        }
+    }
+    pub fn get<I>(&self, index: I) -> Option<&I::Output>
+    where
+        I: SliceIndex<[Data]>,
+    {
+        match self {
+            Self::Vec(v) => v.get::<I>(index.into()),
+            _ => None,
+        }
+    }
+    pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
+    where
+        I: SliceIndex<[Data]>,
+    {
+        match self {
+            Self::Vec(v) => v.get_mut::<I>(index),
+            _ => None,
+        }
+    }
+    pub fn swap(&mut self, a: usize, b: usize) {
+        match self {
+            Self::Vec(v) => v.swap(a, b),
+            _ => (),
+        }
+    }
+    pub fn reverse(&mut self) {
+        match self {
+            Self::Vec(v) => v.reverse(),
+            _ => (),
+        }
+    }
+    pub fn vec_iter(&self) -> Option<Iter<'_, Data>> {
+        match self {
+            Self::Vec(v) => Some(v.iter()),
+            _ => None,
+        }
+    }
+    pub fn vec_iter_mut(&mut self) -> Option<IterMut<'_, Data>> {
+        match self {
+            Self::Vec(v) => Some(v.iter_mut()),
+            _ => None,
+        }
+    }
     /// This will determine the size of signed or unsigned integers and floats
     /// 
     /// i.e. i32 is 32, u64 is 64, f32 is 32, u128 is 128 (Meanwhile, anything else is 0, String is 0, Bool is 0, etc.)