-
Notifications
You must be signed in to change notification settings - Fork 628
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
&self
vs. &mut self
for spawners
#1669
Comments
Do you mean to change both It seems like there could definitely be a performance benefit from having a When I was playing around with global spawners I ended up using an internal trait for this trait SharedSpawn {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError>;
fn status(&self) -> Result<(), SpawnError>;
}
impl<Sp> SharedSpawn for Sp where for<'a> &'a Sp: Spawn {
fn spawn_obj(mut self: &Self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
Spawn::spawn_obj(&mut self, future)
}
fn status(&self) -> Result<(), SpawnError> {
Spawn::status(&self)
}
} that allowed the global setter function to take in a single instance of the spawner and access that via an fn set_global_spawner<Sp: Send + Sync + 'static>(spawner: Sp) where for<'a> &'a Sp: Spawn The nice thing of this sort of pattern is it allows for both unique and shared spawners to exist behind the same basic trait, it's up to the implementor to implement it for shared references if they don't require unique access. |
I mean both I don't see a real performance benefit for And given how hard it will be to pass such spawner around I'm not sure you'll actually need a trait for it. |
On a related note I'd still be curious if there is a safe way to pass a mutable reference through pointers in TLS...
I don't see what would prevent the compiler from inlining |
FWIW, the same question is being considered in tokio right now: tokio-rs/tokio#1344 |
I want the |
I don't think the library is in a life-cycle stage that warrants "workarounds" like this: just "fix" (i.e. change) it properly. I don't see any open argument why the spawn traits need |
#1763 doesn't look like a "workaround" to me, it seems like a pretty straightforward way for users and implementers to precisely define whether they need unique access. I just recalled one implementation that does need unique access: |
@Nemo157 one thing with futuresunordered is I think it may become more useful with the combination of |
I think And I really don't think it's gonna be "straightforward [..] for users and implementers to precisely define whether they need unique access". As a "user" of the trait (i.e. an interface that wants a "Spawner" as input), do I use "UniqueSpawn" or "SharedSpawn"? Maybe right now I don't need to share it, so "UniqueSpawn" seems acceptable, but it'll limit me "forever" (until I break the API). That is why I think adding more traits without good reason is a bad design choice. |
|
I did find the trait argument suggestion in tokio-rs/tokio#1344 pretty compelling, though that could perhaps be accomplished via a second trait and blanked impl, but that's pretty uncomfortable. |
@cramertj I think I already pointed out the solution to |
Also I'd be willing to (try to) PR this it if this sounds acceptable. |
Ah, I see what you're saying now. Yes, that makes sense to me, and I'll put together a PR. My one concern would be whether users would want to have a reference to a |
Hi,
I'd like to reopen this discussion (#767 before for executors).
@aturon wrote in #767 (comment)
Exactly! The strategy is already baked in (
Spawn
/LocalSpawn
implementations usually implementClone
too!) - so there is really no need for&mut self
.It would be way safer though to store a
&self
reference in TLS for the "current spawner".The text was updated successfully, but these errors were encountered: