rstd::counting
All checks were successful
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo clippy (1.65) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.

This commit is contained in:
AF 2023-10-15 11:27:18 +00:00
parent c2304cc9d9
commit 988764ab07
3 changed files with 35 additions and 34 deletions

View File

@ -11,6 +11,7 @@ use crate::rcore::*;
pub mod atomic;
pub mod atomic_object;
pub mod collections;
pub mod counting;
pub mod inject;
pub mod inlining;
mod local_origin;

32
src/rstd/counting.rs Normal file
View File

@ -0,0 +1,32 @@
use crate::func::*;
pub type CountedInstance = instances::effect::EffectInstance<usize>;
impl instances::effect::Effect for usize {
fn e_pure() -> Self {
0
}
fn e_seq(el: Self, er: Self) -> Self {
std::cmp::max(el, er)
}
fn e_after(self, effect: Self) -> Self {
self + effect
}
}
pub type Counted<A> = instances::effect::WithEffect<A, usize>;
impl<A> Counted<A> {
pub fn after_resolution(self) -> Self {
Counted {
value: self.value,
effect: self.effect + 1,
}
}
pub fn count(&self) -> usize {
self.effect
}
}

View File

@ -1,7 +1,6 @@
use std::cmp::max;
use crate::func::{context::*, *};
use crate::rcore::*;
use crate::rstd::counting::CountedInstance;
use super::*;
@ -25,37 +24,6 @@ impl<'a> Context<'a> for TestContextCounted {
}
}
pub type CountedInstance = instances::effect::EffectInstance<usize>;
impl instances::effect::Effect for usize {
fn e_pure() -> Self {
0
}
fn e_seq(el: Self, er: Self) -> Self {
max(el, er)
}
fn e_after(self, effect: Self) -> Self {
self + effect
}
}
pub type Counted<A> = instances::effect::WithEffect<A, usize>;
impl<A> Counted<A> {
fn add(self, n: usize) -> Self {
Counted {
value: self.value,
effect: self.effect + n,
}
}
pub fn count(&self) -> usize {
self.effect
}
}
struct CountedInject;
impl<'a> Inject<'a, TestContextCounted> for CountedInject {
@ -63,7 +31,7 @@ impl<'a> Inject<'a, TestContextCounted> for CountedInject {
&self,
fa: Wrapped<'a, TestContextCounted, A>,
) -> Wrapped<'a, TestContextCounted, A> {
fa.add(1)
fa.after_resolution()
}
}