mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
output: add Memo
This commit is contained in:
parent
f714302f21
commit
2048dd2263
2 changed files with 31 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ mod align; pub use self::align::*;
|
||||||
mod bsp; pub use self::bsp::*;
|
mod bsp; pub use self::bsp::*;
|
||||||
mod cond; pub use self::cond::*;
|
mod cond; pub use self::cond::*;
|
||||||
mod map; pub use self::map::*;
|
mod map; pub use self::map::*;
|
||||||
|
mod memo; pub use self::memo::*;
|
||||||
mod stack; pub use self::stack::*;
|
mod stack; pub use self::stack::*;
|
||||||
//mod reduce; pub use self::reduce::*;
|
//mod reduce; pub use self::reduce::*;
|
||||||
mod thunk; pub use self::thunk::*;
|
mod thunk; pub use self::thunk::*;
|
||||||
|
|
|
||||||
30
output/src/ops/memo.rs
Normal file
30
output/src/ops/memo.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
use crate::*;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)] pub struct Memo<T, U> {
|
||||||
|
pub value: T,
|
||||||
|
pub view: Arc<RwLock<U>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: PartialEq, U> Memo<T, U> {
|
||||||
|
pub fn new (value: T, view: U) -> Self {
|
||||||
|
Self { value, view: Arc::new(view.into()) }
|
||||||
|
}
|
||||||
|
pub fn update <R> (
|
||||||
|
&mut self,
|
||||||
|
newval: T,
|
||||||
|
render: impl Fn(&mut U, &T, &T)->R
|
||||||
|
) -> Option<R> {
|
||||||
|
if newval != self.value {
|
||||||
|
let result = render(&mut*self.view.write().unwrap(), &newval, &self.value);
|
||||||
|
self.value = newval;
|
||||||
|
return Some(result);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear a pre-allocated buffer, then write into it.
|
||||||
|
#[macro_export] macro_rules! rewrite {
|
||||||
|
($buf:ident, $($rest:tt)*) => { |$buf,_,_|{ $buf.clear(); write!($buf, $($rest)*) } }
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue