Files
dcb/dcb-bytes/src/bytes.rs
Filipe Rodrigues 2a39f8b438 Moved Bytes interface to dcb-bytes crate.
Instead of implementing `Bytes for Option<T>`, a proxy is now used, as `dcb-bytes` is in a separate crate.
2020-09-20 02:34:39 +01:00

40 lines
882 B
Rust

//! Interface for converting various structures to and from bytes
// Imports
use std::error::Error;
/// Conversions to and from bytes for the game file
pub trait Bytes
where
Self: Sized,
{
/// The type of array required by this structure
type ByteArray: ByteArray;
/// The error type used for the operation
type FromError: Error;
/// The error type used for the operation
type ToError: Error;
/// Constructs this structure from `bytes`
fn from_bytes(bytes: &Self::ByteArray) -> Result<Self, Self::FromError>;
/// Writes this structure to `bytes`
fn to_bytes(&self, bytes: &mut Self::ByteArray) -> Result<(), Self::ToError>;
}
/// A trait for restricting `Bytes::ByteArray`
pub trait ByteArray {
/// Size of this array
const SIZE: usize;
}
impl<const N: usize> ByteArray for [u8; N] {
const SIZE: usize = N;
}
impl ByteArray for u8 {
const SIZE: usize = 1;
}