mirror of
https://github.com/Noratrieb/safe-stuff.git
synced 2026-01-14 08:25:03 +01:00
adaptors
This commit is contained in:
parent
e67600e818
commit
a8207cfdd9
2 changed files with 49 additions and 0 deletions
3
.rustfmt.toml
Normal file
3
.rustfmt.toml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
imports_granularity = "Crate"
|
||||
newline_style = "Unix"
|
||||
group_imports = "StdExternalCrate"
|
||||
46
src/strategy.rs
Normal file
46
src/strategy.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use core::marker::PhantomData;
|
||||
|
||||
use stuff::Backend;
|
||||
|
||||
pub trait SafeStuffingStrategy<B> {
|
||||
type Extra;
|
||||
|
||||
fn stuff_extra(extra: &Self::Extra) -> B;
|
||||
|
||||
fn extract_extra(addr: B) -> Option<Self::Extra>;
|
||||
}
|
||||
|
||||
/// An internal type to convert from the safe to the unsafe trait
|
||||
pub(crate) struct SafeStrategyAdaptor<S>(PhantomData<S>);
|
||||
|
||||
// SAFETY: `extract_extra` panics if an invalid extra is returned
|
||||
// `stuff_extra` never drops the extra
|
||||
// pointer stuffing is just identity
|
||||
unsafe impl<S, B> stuff::StuffingStrategy<B> for SafeStrategyAdaptor<S>
|
||||
where
|
||||
S: SafeStuffingStrategy<B>,
|
||||
{
|
||||
type Extra = S::Extra;
|
||||
|
||||
fn is_extra(data: B) -> bool {
|
||||
S::extract_extra(data).is_some()
|
||||
}
|
||||
|
||||
fn stuff_extra(inner: Self::Extra) -> B {
|
||||
let b = S::stuff_extra(&inner);
|
||||
core::mem::forget(inner);
|
||||
b
|
||||
}
|
||||
|
||||
unsafe fn extract_extra(data: B) -> Self::Extra {
|
||||
S::extract_extra(data).unwrap()
|
||||
}
|
||||
|
||||
fn stuff_ptr(addr: usize) -> B {
|
||||
addr
|
||||
}
|
||||
|
||||
fn extract_ptr(inner: B) -> usize {
|
||||
inner
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue