Add 'raw' lamination types
This commit is contained in:
parent
e2bec2e83e
commit
1d29cf2cfa
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "byte_lamination"
|
name = "byte_lamination"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Olivier 'reivilibre'"]
|
authors = ["Olivier 'reivilibre'"]
|
||||||
repository = "https://git.emunest.net/reivilibre/byte_lamination.git"
|
repository = "https://git.emunest.net/reivilibre/byte_lamination.git"
|
||||||
|
@ -4,6 +4,8 @@ use std::error::Error;
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use crate::{AutoDelaminate, AutoLaminate, ByteLamination};
|
use crate::{AutoDelaminate, AutoLaminate, ByteLamination};
|
||||||
|
|
||||||
|
/// Wrapper that performs Zstd (de)compression on the bytes.
|
||||||
|
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
|
||||||
pub struct Zstd<'a, T> {
|
pub struct Zstd<'a, T> {
|
||||||
bytes: Cow<'a, [u8]>,
|
bytes: Cow<'a, [u8]>,
|
||||||
_marker: PhantomData<T>,
|
_marker: PhantomData<T>,
|
||||||
@ -24,7 +26,19 @@ impl<'a, T> Into<Cow<'a, [u8]>> for Zstd<'a, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> ByteLamination<'a> for Zstd<'a, T> {}
|
impl<'a, T> ByteLamination<'a> for Zstd<'a, T> {
|
||||||
|
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
|
||||||
|
Cow::Borrowed(&self.bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_bytes(self) -> Vec<u8> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(Self::from(bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Default Zstd compression level used for the AutoLaminate helper.
|
/// Default Zstd compression level used for the AutoLaminate helper.
|
||||||
pub const DEFAULT_ZSTD_LEVEL: i32 = 12;
|
pub const DEFAULT_ZSTD_LEVEL: i32 = 12;
|
||||||
@ -50,9 +64,10 @@ impl<'a, T> Zstd<'a, T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<'a, T: AutoLaminate<U> + ByteLamination<'a>, U> AutoLaminate<U> for Zstd<'a, T> {
|
impl<'a, T: AutoLaminate<U> + ByteLamination<'a> + 'a, U> AutoLaminate<U> for Zstd<'a, T> {
|
||||||
fn laminate(item: U) -> Result<Self, Box<dyn Error>> {
|
fn laminate(item: U) -> Result<Self, Box<dyn Error>> {
|
||||||
let bytes: Cow<'a, [u8]> = T::laminate(item)?.into();
|
let lamination = T::laminate(item)?;
|
||||||
|
let bytes: Cow<'_, [u8]> = lamination.as_cow_bytes();
|
||||||
Zstd::compress(&bytes, DEFAULT_ZSTD_LEVEL)
|
Zstd::compress(&bytes, DEFAULT_ZSTD_LEVEL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,6 +76,6 @@ impl<'a, T: AutoDelaminate<U> + ByteLamination<'a>, U> AutoDelaminate<U> for Zst
|
|||||||
fn delaminate(self) -> Result<U, Box<dyn Error>> {
|
fn delaminate(self) -> Result<U, Box<dyn Error>> {
|
||||||
let memory_limit = max(DEFAULT_MEMORY_LIMIT_MINIMUM, DEFAULT_MEMORY_LIMIT_MULTIPLIER * self.bytes.len());
|
let memory_limit = max(DEFAULT_MEMORY_LIMIT_MINIMUM, DEFAULT_MEMORY_LIMIT_MULTIPLIER * self.bytes.len());
|
||||||
let bytes = self.decompress(memory_limit)?;
|
let bytes = self.decompress(memory_limit)?;
|
||||||
T::from(Cow::Owned(bytes)).delaminate()
|
T::try_from_bytes(Cow::Owned(bytes))?.delaminate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
|
pub mod raw;
|
||||||
pub mod compression;
|
pub mod compression;
|
||||||
pub mod serialisation;
|
pub mod serialisation;
|
||||||
|
|
||||||
@ -42,6 +43,12 @@ pub trait AutoDelaminateBorrowed<'a, T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub trait ByteLamination<'a>: From<Cow<'a, [u8]>> + Into<Cow<'a, [u8]>> {
|
pub trait ByteLamination<'a>: Sized {
|
||||||
|
/// Get the raw bytes from this lamination.
|
||||||
|
/// If possible, this should return borrowed bytes.
|
||||||
|
fn as_cow_bytes(&self) -> Cow<'_, [u8]>;
|
||||||
|
|
||||||
|
fn into_bytes(self) -> Vec<u8>;
|
||||||
|
|
||||||
|
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>>;
|
||||||
}
|
}
|
||||||
|
73
src/raw.rs
Normal file
73
src/raw.rs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
|
use std::error::Error;
|
||||||
|
use crate::ByteLamination;
|
||||||
|
|
||||||
|
impl<'a> ByteLamination<'a> for Cow<'a, [u8]> {
|
||||||
|
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
|
||||||
|
Cow::Borrowed(self.as_ref())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_bytes(self) -> Vec<u8> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ByteLamination<'a> for Vec<u8> {
|
||||||
|
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
|
||||||
|
Cow::Borrowed(self.as_ref())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_bytes(self) -> Vec<u8> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(bytes.into_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ByteLamination<'a> for String {
|
||||||
|
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
|
||||||
|
Cow::Borrowed(self.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_bytes(self) -> Vec<u8> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(match bytes {
|
||||||
|
Cow::Borrowed(bslice) => {
|
||||||
|
std::str::from_utf8(bslice)?.to_owned()
|
||||||
|
}
|
||||||
|
Cow::Owned(bvec) => {
|
||||||
|
String::from_utf8(bvec)?
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ByteLamination<'a> for Cow<'a, str> {
|
||||||
|
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
|
||||||
|
Cow::Borrowed(self.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_bytes(self) -> Vec<u8> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(match bytes {
|
||||||
|
Cow::Borrowed(bslice) => {
|
||||||
|
Cow::Borrowed(std::str::from_utf8(bslice)?)
|
||||||
|
}
|
||||||
|
Cow::Owned(bvec) => {
|
||||||
|
Cow::Owned(String::from_utf8(bvec)?)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@ use serde::de::DeserializeOwned;
|
|||||||
use crate::{AutoDelaminate, AutoLaminate, ByteLamination};
|
use crate::{AutoDelaminate, AutoLaminate, ByteLamination};
|
||||||
|
|
||||||
/// Wrapper that uses serde to perform BARE serialisation on an arbitrary type.
|
/// Wrapper that uses serde to perform BARE serialisation on an arbitrary type.
|
||||||
|
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
|
||||||
pub struct SerdeBare<'a, T> {
|
pub struct SerdeBare<'a, T> {
|
||||||
bytes: Cow<'a, [u8]>,
|
bytes: Cow<'a, [u8]>,
|
||||||
_marker: PhantomData<T>,
|
_marker: PhantomData<T>,
|
||||||
@ -26,7 +27,19 @@ impl<'a, T> Into<Cow<'a, [u8]>> for SerdeBare<'a, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> ByteLamination<'a> for SerdeBare<'a, T> {}
|
impl<'a, T> ByteLamination<'a> for SerdeBare<'a, T> {
|
||||||
|
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
|
||||||
|
Cow::Borrowed(&self.bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_bytes(self) -> Vec<u8> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(Self::from(bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, T: Serialize> SerdeBare<'a, T> {
|
impl<'a, T: Serialize> SerdeBare<'a, T> {
|
||||||
pub fn serialise<'inp>(inner: &'inp T) -> Result<Self, Box<dyn Error>> {
|
pub fn serialise<'inp>(inner: &'inp T) -> Result<Self, Box<dyn Error>> {
|
||||||
|
@ -6,6 +6,7 @@ use serde::de::DeserializeOwned;
|
|||||||
use crate::{AutoDelaminate, AutoDelaminateBorrowed, AutoLaminate, ByteLamination};
|
use crate::{AutoDelaminate, AutoDelaminateBorrowed, AutoLaminate, ByteLamination};
|
||||||
|
|
||||||
/// Wrapper that uses serde to perform CBOR serialisation on an arbitrary type.
|
/// Wrapper that uses serde to perform CBOR serialisation on an arbitrary type.
|
||||||
|
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
|
||||||
pub struct SerdeCbor<'a, T> {
|
pub struct SerdeCbor<'a, T> {
|
||||||
bytes: Cow<'a, [u8]>,
|
bytes: Cow<'a, [u8]>,
|
||||||
_marker: PhantomData<T>,
|
_marker: PhantomData<T>,
|
||||||
@ -26,7 +27,19 @@ impl<'a, T> Into<Cow<'a, [u8]>> for SerdeCbor<'a, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> ByteLamination<'a> for SerdeCbor<'a, T> {}
|
impl<'a, T> ByteLamination<'a> for SerdeCbor<'a, T> {
|
||||||
|
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
|
||||||
|
Cow::Borrowed(&self.bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_bytes(self) -> Vec<u8> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(Self::from(bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, T: Serialize> SerdeCbor<'a, T> {
|
impl<'a, T: Serialize> SerdeCbor<'a, T> {
|
||||||
pub fn serialise<'inp>(inner: &'inp T) -> Result<Self, Box<dyn Error>> {
|
pub fn serialise<'inp>(inner: &'inp T) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Loading…
Reference in New Issue
Block a user