Trait npyz::Serialize [−][src]
pub trait Serialize { type TypeWriter: TypeWrite<Value = Self>; fn writer(dtype: &DType) -> Result<Self::TypeWriter, DTypeError>; }
Expand description
Trait that permits writing a type to an .npy
file.
Examples of types that implement this:
- Primitive integers, floats,
Complex
(with the"complex"
feature) - Byte slices (
[u8]
)
This trait is derivable when enabling the "derive"
feature. This makes it easier
to work with structured arrays.
For an example of how to implement this manually, see Vector5
in the
roundtrip test.
Associated Types
type TypeWriter: TypeWrite<Value = Self>
type TypeWriter: TypeWrite<Value = Self>
Think of this as some sort of for<W: io::Write> Fn(W, &Self) -> io::Result<()>
.
There is no closure-like sugar for these; you must manually define a type that
implements TypeWrite
.
Required methods
fn writer(dtype: &DType) -> Result<Self::TypeWriter, DTypeError>
fn writer(dtype: &DType) -> Result<Self::TypeWriter, DTypeError>
Get a function that serializes a single data field at a time.
The purpose of the dtype
arugment is to allow e.g. specifying a length for string types,
or the endianness for integers. The derivable AutoSerialize
trait is able to supply
many types with a reasonable default.
Errors
Returns Err
if the DType
is not compatible with Self
.
Implementations on Foreign Types
type TypeWriter = PrimitiveWriter<i8>
type TypeWriter = PrimitiveWriter<i16>
type TypeWriter = PrimitiveWriter<i32>
type TypeWriter = PrimitiveWriter<i64>
type TypeWriter = PrimitiveWriter<u8>
type TypeWriter = PrimitiveWriter<u16>
type TypeWriter = PrimitiveWriter<u32>
type TypeWriter = PrimitiveWriter<u64>
type TypeWriter = PrimitiveWriter<f32>
type TypeWriter = PrimitiveWriter<f64>
type TypeWriter = BytesWriter
type TypeWriter = TypeWriteViaDeref<Vec<u8>>
type TypeWriter = TypeWriteViaDeref<&'a T>
type TypeWriter = TypeWriteViaDeref<&'a mut T>
type TypeWriter = TypeWriteViaDeref<Box<T>>
type TypeWriter = TypeWriteViaDeref<Rc<T>>
type TypeWriter = TypeWriteViaDeref<Arc<T>>
type TypeWriter = TypeWriteViaDeref<Cow<'a, T>>
type TypeWriter = ArrayWriter<<T as Serialize>::TypeWriter, N>