feat: initial commit
This commit is contained in:
commit
2c6f232fb1
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
/Cargo.lock
|
10
Cargo.toml
Normal file
10
Cargo.toml
Normal 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
9
src/foo_imaginary.rs
Normal 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
24
src/foo_ir.rs
Normal 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
22
src/foo_with.rs
Normal 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
3
src/lib.rs
Normal file
@ -0,0 +1,3 @@
|
||||
mod foo_ir;
|
||||
mod foo_with;
|
||||
mod tracing_level;
|
23
src/tracing_level.rs
Normal file
23
src/tracing_level.rs
Normal 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(()),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user