mirror of
https://github.com/Zenithsiz/dcb.git
synced 2026-02-09 11:48:16 +00:00
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.
This commit is contained in:
39
dcb-bytes/src/bytes.rs
Normal file
39
dcb-bytes/src/bytes.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
//! 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;
|
||||
}
|
||||
Reference in New Issue
Block a user