-
-
Notifications
You must be signed in to change notification settings - Fork 4
Switch SideEffect
to GAT lifetime
#3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
And in case I forget: this issue is what is blocking the improved |
TODO: try getting around this with a manual trait impl of SideEffect instead of relying on closure/fn higher ranked lifetimes |
Tried the following as a workaround: pub struct Raw<T>(T);
impl<T: Send + 'static> SideEffect for Raw<T> {
type Api<'a> = (&'a mut T, impl CData + Fn(Box<dyn FnOnce(&mut T)>));
fn build(self, registrar: SideEffectRegistrar<'_>) -> Self::Api<'_> {
registrar.raw(self.0)
}
}
pub fn as_listener() -> impl for<'a> SideEffect<Api<'a> = ()> {
|_: SideEffectRegistrar| {}
}
pub struct State<T>(pub T);
impl<T: Send + 'static> SideEffect for State<T> {
type Api<'a> = (&'a mut T, impl CData + Fn(T));
fn build(self, registrar: SideEffectRegistrar<'_>) -> Self::Api<'_> {
let (state, rebuild) = registrar.raw(self.0);
let set_state = move |new_state| {
rebuild(Box::new(|state| *state = new_state));
};
(state, set_state)
}
} Didn't work because:
So, really looks like this issue is impossible to achieve until Rust has some substantial compiler bug fixes. |
SideEffect
to no trait lifetimesSideEffect
to no trait lifetimes (opting for GAT lifetime)
SideEffect
to no trait lifetimes (opting for GAT lifetime)SideEffect
to GAT lifetime
This seems to compile. It's a newtype around the closure instead of just the initial state. fn fix_lifetime<F, T, R1, R2>(f: F) -> F
where
F: for<'a> FnOnce(SideEffectRegistrar<'a>) -> (&'a mut T, R1, R2),
{
f
}
pub struct Raw<F>(F);
impl<T, F, R1, R2> SideEffect for Raw<F>
where
T: Send + 'static,
F: FnOnce(SideEffectRegistrar<'_>) -> (&mut T, R1, R2),
{
type Api<'a> = (&'a mut T, R1, R2);
fn build<'a>(self, registrar: SideEffectRegistrar<'a>) -> Self::Api<'a> {
self.0(registrar)
}
}
pub fn raw<T: Send + 'static>(
initial: T,
) -> impl for<'a> SideEffect<
Api<'a> = (
&'a mut T,
impl CData + Fn(Box<dyn FnOnce(&mut T)>),
Arc<dyn Send + Sync + Fn(Box<dyn FnOnce()>)>,
),
> {
Raw(
fix_lifetime(move |register: SideEffectRegistrar<'_>| register.raw(initial))
)
} |
@0e4ef622 Huh! I am super impressed that (1) you were able to adapt the code samples I gave above to accommodate the changes I've made to side effects since then and (2) modify those changes into something that actually compiles. Thanks for sharing that snippet! I'll take a closer look into this workaround... SideEffect GATs were the last "big thing" preventing ReArch's 1.0, so this would be great to have resolved. (The only other thing separating ReArch-rs from a 1.0 is the desire to get a few experienced Rust devs out there to do a code review to make sure I followed best-practices and exposed a forward-thinking API; I did the best I could but a more seasoned set of eyes is always a plus.) |
Side effects, when expressed correctly, should just be functions that work for any given
SideEffectRegistrar<'a>
and should themselves not be dependent upon any lifetime (i.e., theSideEffectRegistrar
itself). However, due to limitations in Rust's current implementation of higher-ranked lifetimes and some other related weird type-related bugs I've found along the way, this is not possible at the moment and the current (technically incorrect) implementation of side effects was born (they work, just not optimally). Rust just isn't quite ready for some of the odd, edge-case stuff I am trying to put it through.Here's some sample code I was working on that properly expresses side effects, for when the time comes that it is possible:
And for future reference, if I need it, here's some side-effect code I was experimenting with using the correct approach.
The text was updated successfully, but these errors were encountered: