Skip to content

Commit 7e6e585

Browse files
committed
Experiment with "owning memo"
U cannot get this to work. It's close but I think it requires lifetime bounds on the HRTB lifetimes e.g. `F: for<'a, 'store: 'a> ...`, which currently isn't possible (seems to have been possible at one point? rust-lang/rust#50555 Could also be just that the syntax didn't result in an error, but had no semantic meaning attached to it.)
1 parent 72fd009 commit 7e6e585

File tree

7 files changed

+113
-23
lines changed

7 files changed

+113
-23
lines changed

src/memo/cell.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ impl<C, S, T> CellMemo<C, S>
3030
}
3131
}
3232

33-
impl<C, S, T> Memo for CellMemo<C, S>
33+
impl<C, S, T: 'static> Memo for CellMemo<C, S>
3434
where
3535
C: TypeConstructor,
3636
S: for<'a, 'store> Fn(&'a C::Type<'store>, ReadContext<'store>) -> &'a VersionedCell<'store, T>
3737
+ Clone,
3838
{
3939
type RootTC = C;
40-
type Target<'store> = VersionedCell<'store, T>;
40+
type Target<'a, 'store: 'a> = &'a VersionedCell<'store, T>;
4141
type Selector = CellSelector<C, S>;
4242

43-
fn refresh<'a, 'store>(
43+
fn refresh<'a, 'store: 'a>(
4444
&mut self,
4545
root: &'a C::Type<'store>,
4646
cx: ReadContext<'store>,
47-
) -> Refresh<&'a Self::Target<'store>> {
47+
) -> Refresh<Self::Target<'a, 'store>> {
4848
if cx.store_id() != self.store_id {
4949
panic!("cannot resolve selector against different store")
5050
}
@@ -75,19 +75,19 @@ pub struct CellSelector<C, S> {
7575
_marker: marker::PhantomData<*const C>,
7676
}
7777

78-
impl<C, S, T> Selector for CellSelector<C, S>
78+
impl<C, S, T: 'static> Selector for CellSelector<C, S>
7979
where
8080
C: TypeConstructor,
8181
S: for<'a, 'store> Fn(&'a C::Type<'store>, ReadContext<'store>) -> &'a VersionedCell<'store, T>,
8282
{
8383
type RootTC = C;
84-
type Target<'store> = VersionedCell<'store, T>;
84+
type Target<'a, 'store: 'a> = &'a VersionedCell<'store, T>;
8585

86-
fn select<'a, 'store>(
86+
fn select<'a, 'store: 'a>(
8787
&self,
8888
root: &'a C::Type<'store>,
8989
cx: ReadContext<'store>,
90-
) -> &'a Self::Target<'store> {
90+
) -> Self::Target<'a, 'store> {
9191
(self.lens)(root, cx)
9292
}
9393
}

src/memo/memo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ impl<T> Deref for Refresh<T> {
3333
pub trait Memo {
3434
type RootTC: TypeConstructor;
3535

36-
type Target<'store>;
36+
type Target<'a, 'store: 'a>;
3737

38-
type Selector: for<'store> Selector<RootTC = Self::RootTC, Target<'store> = Self::Target<'store>>;
38+
type Selector: Selector;
3939

40-
fn refresh<'a, 'store>(
40+
fn refresh<'a, 'store: 'a>(
4141
&mut self,
4242
root: &'a <Self::RootTC as TypeConstructor>::Type<'store>,
4343
cx: ReadContext<'store>,
44-
) -> Refresh<&'a Self::Target<'store>>;
44+
) -> Refresh<Self::Target<'a, 'store>>;
4545

4646
fn selector(&self) -> Self::Selector;
4747
}

src/memo/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub use self::selector::*;
66

77
pub mod cell;
88
pub mod node;
9+
pub mod owned;
910

1011
fn test() {
1112
use futures::StreamExt;

src/memo/node.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ impl<N, C, S> Memo for NodeMemo<N, C, S>
4242
+ Clone,
4343
{
4444
type RootTC = C;
45-
type Target<'store> = VersionedCell<'store, N::Type<'store>>;
45+
type Target<'a, 'store: 'a> = &'a VersionedCell<'store, N::Type<'store>>;
4646
type Selector = NodeSelector<N, C, S>;
4747

48-
fn refresh<'a, 'store>(
48+
fn refresh<'a, 'store: 'a>(
4949
&mut self,
5050
root: &'a C::Type<'store>,
5151
cx: ReadContext<'store>,
52-
) -> Refresh<&'a Self::Target<'store>> {
52+
) -> Refresh<Self::Target<'a, 'store>> {
5353
if cx.store_id() != self.store_id {
5454
panic!("cannot resolve selector against different store")
5555
}
@@ -89,13 +89,13 @@ impl<N, C, S> Selector for NodeSelector<N, C, S>
8989
S: for<'a, 'store> Fn(&'a C::Type<'store>, ReadContext<'store>) -> &'a VersionedCell<'store, N::Type<'store>>,
9090
{
9191
type RootTC = C;
92-
type Target<'store> = VersionedCell<'store, N::Type<'store>>;
92+
type Target<'a, 'store: 'a> = &'a VersionedCell<'store, N::Type<'store>>;
9393

94-
fn select<'a, 'store>(
94+
fn select<'a, 'store: 'a>(
9595
&self,
9696
root: &'a C::Type<'store>,
9797
cx: ReadContext<'store>,
98-
) -> &'a Self::Target<'store> {
98+
) -> Self::Target<'a, 'store> {
9999
(self.lens)(root, cx)
100100
}
101101
}

src/memo/owned.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use std::sync::atomic::AtomicU64;
2+
use std::marker;
3+
use crate::TypeConstructor;
4+
use crate::store::{ReadContext, Store};
5+
use crate::versioned_cell::VersionedCell;
6+
use crate::memo::{Memo, Refresh, Selector};
7+
use std::sync::atomic;
8+
9+
pub struct OwnedMemo<C, S, T> {
10+
select: S,
11+
store_id: u64,
12+
last: T,
13+
_marker: marker::PhantomData<*const C>,
14+
}
15+
16+
impl<C, S, T> OwnedMemo<C, S, T>
17+
where
18+
C: TypeConstructor,
19+
S: for<'a, 'store> Fn(&'a C::Type<'store>, ReadContext<'store>) -> T,
20+
{
21+
pub fn new(store: &Store<C>, select: S) -> Self {
22+
let last = store.with(|root, cx| select(root, cx));
23+
24+
OwnedMemo {
25+
select,
26+
store_id: store.id(),
27+
last,
28+
_marker: marker::PhantomData,
29+
}
30+
}
31+
}
32+
33+
// impl<C, S, T> Memo for OwnedMemo<C, S>
34+
// where
35+
// C: TypeConstructor,
36+
// S: for<'a, 'store> Fn(&'a C::Type<'store>, ReadContext<'store>) -> &'a VersionedCell<'store, T>
37+
// + Clone,
38+
// {
39+
// type RootTC = C;
40+
// type Target<'store> = VersionedCell<'store, T>;
41+
// type Selector = CellSelector<C, S>;
42+
//
43+
// fn refresh<'a, 'store>(
44+
// &mut self,
45+
// root: &'a C::Type<'store>,
46+
// cx: ReadContext<'store>,
47+
// ) -> Refresh<&'a Self::Target<'store>> {
48+
// if cx.store_id() != self.store_id {
49+
// panic!("cannot resolve selector against different store")
50+
// }
51+
//
52+
// let cell = (self.select)(root, cx);
53+
// let version = cell.version();
54+
// let last_version = self.last_version;
55+
//
56+
// self.last_version = version;
57+
//
58+
// if version == last_version {
59+
// Refresh::Unchanged(cell)
60+
// } else {
61+
// Refresh::Changed(cell)
62+
// }
63+
// }
64+
//
65+
// fn selector(&self) -> Self::Selector {
66+
// CellSelector {
67+
// lens: self.select.clone(),
68+
// _marker: marker::PhantomData,
69+
// }
70+
// }
71+
// }
72+
73+
pub struct OwnedSelector<C, T> {
74+
value: T,
75+
_marker: marker::PhantomData<*const C>
76+
}
77+
78+
impl<C, T> Selector for OwnedSelector<C, T> where C: TypeConstructor, T: Clone {
79+
type RootTC = C;
80+
type Target<'a, 'store: 'a> = T;
81+
82+
fn select<'a, 'store: 'a>(
83+
&self,
84+
root: &'a C::Type<'store>,
85+
cx: ReadContext<'store>,
86+
) -> T {
87+
self.value.clone()
88+
}
89+
}

src/memo/selector.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use crate::store::ReadContext;
44
pub trait Selector {
55
type RootTC: TypeConstructor;
66

7-
type Target<'store>;
7+
type Target<'a, 'store: 'a>;
88

9-
fn select<'a, 'store>(
9+
fn select<'a, 'store: 'a>(
1010
&self,
1111
root: &'a <Self::RootTC as TypeConstructor>::Type<'store>,
1212
cx: ReadContext<'store>,
13-
) -> &'a Self::Target<'store>;
13+
) -> Self::Target<'a, 'store>;
1414
}

src/watcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<C, S> View<C, S>
6464
{
6565
pub fn with<F, R>(&self, f: F) -> R
6666
where
67-
F: for<'store> FnOnce(&S::Target<'store>, ReadContext<'store>) -> R,
67+
F: for<'a, 'store> FnOnce(S::Target<'a, 'store>, ReadContext<'store>) -> R,
6868
{
6969
self.store.with(|root, cx| f(self.selector.select(root, cx), cx))
7070
}
@@ -157,7 +157,7 @@ impl<C, M0, M1> View2<C, M0, M1>
157157
{
158158
pub fn with<F, R>(&self, f: F) -> R
159159
where
160-
F: for<'store> FnOnce((&M0::Target<'store>, &M1::Target<'store>), ReadContext<'store>) -> R,
160+
F: for<'a, 'store> FnOnce((M0::Target<'a, 'store>, M1::Target<'a, 'store>), ReadContext<'store>) -> R,
161161
{
162162
self.store.with(|root, cx| {
163163
let selector_0 = self.selector_0.select(root, cx);

0 commit comments

Comments
 (0)