feat: initial commit

This commit is contained in:
Orion Kindel 2023-07-03 19:26:26 -05:00
commit 2c6f232fb1
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
7 changed files with 93 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/Cargo.lock

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "rkyv_test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rkyv = "*"
tracing = "*"

9
src/foo_imaginary.rs Normal file
View File

@ -0,0 +1,9 @@
// note: does not compile
#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
pub struct Foo {
/// hypothetical attribute allowing a free function of signature
/// `Fn(<field_type>) -> <dest_type>` where `<dest_type>` implements `Archive`
#[archive_using(crate::tracing_level::to_u8)]
level: tracing::Level,
}

24
src/foo_ir.rs Normal file
View File

@ -0,0 +1,24 @@
pub struct Foo {
level: tracing::Level,
}
#[derive(rkyv::Deserialize, rkyv::Serialize, rkyv::Archive)]
pub struct FooIR {
level: u8,
}
impl From<Foo> for FooIR {
fn from(value: Foo) -> Self {
FooIR {
level: crate::tracing_level::to_u8(value.level),
}
}
}
impl From<FooIR> for Foo {
fn from(value: FooIR) -> Self {
Foo {
level: crate::tracing_level::try_from_u8(value.level).unwrap(),
}
}
}

22
src/foo_with.rs Normal file
View File

@ -0,0 +1,22 @@
use rkyv::with::ArchiveWith;
#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
pub struct Foo {
#[with(TracingLevel)]
level: tracing::Level,
}
pub struct TracingLevel;
impl ArchiveWith<tracing::Level> for TracingLevel {
type Archived = u8;
type Resolver = ();
unsafe fn resolve_with(
field: &tracing::Level,
_: usize,
_: Self::Resolver,
out: *mut Self::Archived,
) {
out.write(crate::tracing_level::to_u8(*field))
}
}

3
src/lib.rs Normal file
View File

@ -0,0 +1,3 @@
mod foo_ir;
mod foo_with;
mod tracing_level;

23
src/tracing_level.rs Normal file
View File

@ -0,0 +1,23 @@
pub fn to_u8(lvl: tracing::Level) -> u8 {
use tracing::Level;
match lvl {
Level::TRACE => 0,
Level::DEBUG => 1,
Level::INFO => 2,
Level::WARN => 3,
Level::ERROR => 4,
}
}
pub fn try_from_u8(lvl: u8) -> Result<tracing::Level, ()> {
use tracing::Level;
match lvl {
0 => Ok(Level::TRACE),
1 => Ok(Level::DEBUG),
2 => Ok(Level::INFO),
3 => Ok(Level::WARN),
4 => Ok(Level::ERROR),
_ => Err(()),
}
}