-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Witty traits for
log::Level
(#1877)
* Add a `log` feature to `linera-witty` Prepare to implement the Witty traits for a type from the `log` crate, but only if requested by the user. * Implement Witty traits for `log::Level` Place it behind a feature gate, so that the `log` dependency is only included if requested. * Test WIT roundtrip of `log::Level` Ensure all variants can be stored in memory and loaded from memory following the WIT specification, as well as lowered into and lifted from its flat layout.
- Loading branch information
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) Zefchain Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Implementations of the custom traits for types from the [`log`] crate. | ||
use std::borrow::Cow; | ||
|
||
use frunk::{hlist_pat, HList}; | ||
use log::Level; | ||
|
||
use crate::{ | ||
GuestPointer, InstanceWithMemory, Layout, Memory, Runtime, RuntimeError, RuntimeMemory, | ||
WitLoad, WitStore, WitType, | ||
}; | ||
|
||
impl WitType for Level { | ||
const SIZE: u32 = 1; | ||
|
||
type Layout = HList![i8]; | ||
type Dependencies = HList![]; | ||
|
||
fn wit_type_name() -> Cow<'static, str> { | ||
"log-level".into() | ||
} | ||
|
||
fn wit_type_declaration() -> Cow<'static, str> { | ||
concat!( | ||
" enum log-level {\n", | ||
" error,\n", | ||
" warn,\n", | ||
" info,\n", | ||
" debug,\n", | ||
" trace,\n", | ||
" }\n", | ||
) | ||
.into() | ||
} | ||
} | ||
|
||
impl WitLoad for Level { | ||
fn load<Instance>( | ||
memory: &Memory<'_, Instance>, | ||
location: GuestPointer, | ||
) -> Result<Self, RuntimeError> | ||
where | ||
Instance: InstanceWithMemory, | ||
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, | ||
{ | ||
match u8::load(memory, location)? { | ||
0 => Ok(Level::Error), | ||
1 => Ok(Level::Warn), | ||
2 => Ok(Level::Info), | ||
3 => Ok(Level::Debug), | ||
4 => Ok(Level::Trace), | ||
_ => unreachable!("Invalid log level"), | ||
} | ||
} | ||
|
||
fn lift_from<Instance>( | ||
hlist_pat![discriminant]: <Self::Layout as Layout>::Flat, | ||
_memory: &Memory<'_, Instance>, | ||
) -> Result<Self, RuntimeError> | ||
where | ||
Instance: InstanceWithMemory, | ||
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, | ||
{ | ||
match discriminant { | ||
0 => Ok(Level::Error), | ||
1 => Ok(Level::Warn), | ||
2 => Ok(Level::Info), | ||
3 => Ok(Level::Debug), | ||
4 => Ok(Level::Trace), | ||
_ => unreachable!("Invalid log level"), | ||
} | ||
} | ||
} | ||
|
||
impl WitStore for Level { | ||
fn store<Instance>( | ||
&self, | ||
memory: &mut Memory<'_, Instance>, | ||
location: GuestPointer, | ||
) -> Result<(), RuntimeError> | ||
where | ||
Instance: InstanceWithMemory, | ||
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, | ||
{ | ||
let discriminant: i8 = match self { | ||
Level::Error => 0, | ||
Level::Warn => 1, | ||
Level::Info => 2, | ||
Level::Debug => 3, | ||
Level::Trace => 4, | ||
}; | ||
|
||
discriminant.store(memory, location) | ||
} | ||
|
||
fn lower<Instance>( | ||
&self, | ||
memory: &mut Memory<'_, Instance>, | ||
) -> Result<<Self::Layout as Layout>::Flat, RuntimeError> | ||
where | ||
Instance: InstanceWithMemory, | ||
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, | ||
{ | ||
let discriminant: i8 = match self { | ||
Level::Error => 0, | ||
Level::Warn => 1, | ||
Level::Info => 2, | ||
Level::Debug => 3, | ||
Level::Trace => 4, | ||
}; | ||
|
||
discriminant.lower(memory) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
mod custom_types; | ||
mod frunk; | ||
#[cfg(with_log)] | ||
mod log; | ||
mod std; | ||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters