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
#[derive(Clone, Debug)] pub struct Vector<T>(Option<Vec<T>>); impl<T> Default for Vector<T> { fn default() -> Self { Vector(None) } } impl<T> From<Vec<T>> for Vector<T> { fn from(vec: Vec<T>) -> Self { Vector(Some(vec)) } } impl<T> From<Option<Vec<T>>> for Vector<T> { fn from(val: Option<Vec<T>>) -> Self { Vector(val) } } impl<T> Vector<T> { pub fn new() -> Self { Vector::default() } pub fn init(&mut self, value: Option<Vec<T>>) { *self = Vector(value); } pub fn push(&mut self, push: T) { match self { Vector(None) => { *self = Vector(Some(vec![push])); } Vector(Some(ref mut v)) => (*v).push(push), } } pub fn is_none(&self) -> bool { match self { Vector(None) => false, Vector(Some(_)) => true, } } pub fn none(&mut self) { (*self) = Vector(None); } }