@SkiFire13 I wasn't aware of clippy
, it helped factorize the code a bit!
I attempted to remove Arc
from the Observable
constructor (now just Observable::new(|observer| { Arc::new(cleanup_fn) })
), but couldn't take it out of the cleanup callback because I don't want to add a generic parameter to Observable
(even if it's inferred). I've struct Observable<T, Error = ()>
; as you can see Error is trailling. And Rust sometimes you'll have to specify all generic parameters, so an additional _
portion for the Cleanup can be confusing?
Sadly the Rust's object initializer currently doesn't allow omitting fields unless using ..Default::default()
, which is verbose. Someone proposed ..
as an alias to that I think.
Can't you just add a generic parameter to Observable::new
? Something like (not tested):
fn new<F, G>(subscriber: F) -> Observable,<T, Error>
where
F: Fn(SubscriptionObserver<T, Error>) -> G + Send + Sync + 'static,
G: Fn() + Send + Sync + 'static
{
Self { observer: Arc::new(|subobserver| Box::new(subscriber(subobserver))) }
}
I see that you used trait aliases, which are an unstable feature. This means that a nightly compiler is required, you should add this to the requirements, since unless specified it's assumed a project is compatible with the latest stable compiler.
1 Like
Your example just missed a move
. Now, yes, no Arc
involved anymore