pub enum Type {
Scalar(ScalarType),
Array(ArrayShape, ScalarType),
Vector(u64, Arc<Type>),
Tuple(Vec<Arc<Type>>),
NamedTuple(Vec<(String, Arc<Type>)>),
}
Expand description
Variants§
Scalar(ScalarType)
Each scalar corresponds to a signed or an unsigned number modulo m
, where m
= {2, 28, 216, 232, 264}.
Scalar types supported by CipherCore are provided as parameters. These scalar types could be BIT, UINT8, INT8, UINT16, INT16, UINT32, INT32, UINT64 or INT64.
§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = scalar_type(BIT);
let i0 = g.input(t.clone()).unwrap();
assert_eq!(i0.get_type().unwrap(), t);
Array(ArrayShape, ScalarType)
Array entries are scalars and arrays could be multi-dimensional.
Array is analogous to NumPy’s numpy.array.
An array is defined using a valid shape and a supported scalar type.
§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let a_shape = vec![10, 1, 20];
let at = array_type(a_shape, UINT32);
assert!(at.is_valid());
let i0 = g.input(at.clone()).unwrap();
assert_eq!(i0.get_type().unwrap(), at);
Vector(u64, Arc<Type>)
Vector type is a one-dimensional list of entries of some constituent type, which can be any valid type including the vector or tuple type.
A vector type is defined by the number of entries and a pointer to the type of those entries.
§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let a_shape = vec![10, 1, 20];
let at = array_type(a_shape, UINT32);
let vt0 = vector_type(10, at);
assert!(vt0.is_valid());
let vt1 = vector_type(10, vt0);
let i0 = g.input(vt1).unwrap();
assert!(i0.get_type().unwrap().is_valid());
Tuple(Vec<Arc<Type>>)
Tuples are fixed-length lists consisting of entries of given type(s).
tuple(T
0, T
1, …, T
n-1) corresponds to a list of types T
0, T
1, …, T
n-1.
tuple() can be interpreted as a void type.
§Example
let a_shape = vec![10, 1, 20];
let at = array_type(a_shape, UINT32);
let vt0 = vector_type(10, at.clone());
let vt1 = vector_type(10, at);
let tt = tuple_type(vec![vt0, vt1]);
assert!(tt.is_valid());
NamedTuple(Vec<(String, Arc<Type>)>)
Named tuples are fixed-length lists that consist of tuples of element-name and associated element-type.
An element’s name can be used to access a particular element within the named tuple.
A valid named tuple should have all the element-types valid.
Element-names for the element-types should be unique.
Named tuple is similar to the tuple type.
§Example
let st = scalar_type(UINT64);
let at = array_type(vec![64], UINT8);
let nt = named_tuple_type(vec![("ID".to_owned(), st), ("Name".to_owned(), at)]);
assert!(nt.is_valid());
Implementations§
Source§impl Type
impl Type
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Tests whether a given type is valid.
Recursively checks if all the arrays that constitute the type have valid shapes, and all the named tuples have unique field names.
§Returns
true
, if type instance and its sub-type(s) are valid, otherwise false
§Example
let s0 = INT16;
let t0 = Type::Scalar(s0.clone());
assert!(t0.is_valid());
let a1 = array_type(vec![2, 0, 2], s0.clone());
assert!(!a1.is_valid());
let a2 = array_type(vec![2, 1, 2], s0);
assert!(a2.is_valid());
Sourcepub fn is_tuple(&self) -> bool
pub fn is_tuple(&self) -> bool
Tests if a type is tuple.
§Returns
true
, if type is a tuple, otherwise false
§Example
let s0 = INT32;
let t0 = Type::Scalar(s0.clone());
assert!(!t0.is_tuple());
let a0 = array_type(vec![2, 1, 2], s0.clone());
assert!(!a0.is_tuple());
let v0 = vector_type(2, a0.clone());
assert!(!v0.is_tuple());
let v1 = vector_type(2, a0);
let t1 = tuple_type(vec![v0, v1]);
assert!(t1.is_tuple());
Sourcepub fn is_named_tuple(&self) -> bool
pub fn is_named_tuple(&self) -> bool
Tests if a type is named tuple.
§Returns
true
, if type is a named tuple, otherwise false
§Example
let s0 = INT16;
let a0 = array_type(vec![2, 1, 2], s0.clone());
assert!(!a0.is_tuple());
let nt0 = named_tuple_type(vec![
("Name".to_owned(), a0.clone()),
("Zip".to_owned(), a0)
]);
assert!(nt0.is_named_tuple());
Sourcepub fn get_scalar_type(&self) -> ScalarType
pub fn get_scalar_type(&self) -> ScalarType
Returns the underlying scalar type of a scalar or an array.
§Panics
Panics if self
is neither a scalar nor an array
§Returns
Copy of the underlying scalar type for a scalar or an array
§Example
let s0 = INT16;
let st0 = Type::Scalar(s0.clone());
let a0 = array_type(vec![2, 1, 2], s0.clone());
assert!(st0.get_scalar_type().eq(&s0));
assert!(a0.get_scalar_type().eq(&s0));
Sourcepub fn get_shape(&self) -> ArrayShape
pub fn get_shape(&self) -> ArrayShape
Sourcepub fn get_dimensions(&self) -> ArrayShape
pub fn get_dimensions(&self) -> ArrayShape
Returns the array shape for an array type or [1] for a scalar type.
§Panics
Panics if self
is neither a scalar nor an array
§Returns
- Copy of the shape of a given array, or
- Shape [1], if a scalar type is given
§Example
let s0 = INT16;
let st0 = Type::Scalar(s0.clone());
let a0_shape = vec![2, 1, 2];
let a0 = array_type(a0_shape.clone(), s0.clone());
assert!(a0.get_dimensions().eq(&a0_shape));
assert_eq!(vec![1], st0.get_dimensions());
Sourcepub fn size_in_bits(&self) -> Result<u64>
pub fn size_in_bits(&self) -> Result<u64>
Returns the size of a type in bits.
Returns a runtime error if type is not valid.
Within named tuple types, the bit size of name strings is omitted.
§Returns
Type size in bits
Source§impl Type
impl Type
Sourcepub fn get_named_types(&self) -> Result<Vec<(String, Type)>>
pub fn get_named_types(&self) -> Result<Vec<(String, Type)>>
Returns a vector of tuples created from the named tuple.
This function reverse the result of named_tuple_type()
.
§Returns
Vector of tuples (type name, type)
§Example
let st0 = Type::Scalar(UINT8);
let st1 = Type::Scalar(UINT32);
let vec_t = vec![("age".to_owned(), st0), ("zip".to_owned(), st1)];
let t = named_tuple_type(vec_t.clone());
assert_eq!(vec_t, t.get_named_types().unwrap());
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Type
impl<'de> Deserialize<'de> for Type
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Type
impl StructuralPartialEq for Type
Auto Trait Implementations§
impl Freeze for Type
impl RefUnwindSafe for Type
impl Send for Type
impl Sync for Type
impl Unpin for Type
impl UnwindSafe for Type
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.