Skip to content
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

impl From<Arc<T>> for web::Data<T> #1300

Closed
SOF3 opened this issue Jan 21, 2020 · 6 comments · Fixed by #1509
Closed

impl From<Arc<T>> for web::Data<T> #1300

SOF3 opened this issue Jan 21, 2020 · 6 comments · Fixed by #1509
Labels
C-improvement Category: an improvement to existing functionality

Comments

@SOF3
Copy link

SOF3 commented Jan 21, 2020

actix-web is just part of my application. The data I am trying to store comes from the upper level, which passes the value around as an Arc<T>. I could only store this value as web::Data<Arc<T>>, but this would become double indirection.

@tyranron
Copy link
Member

@SOF3 would you be so kind to provide a code example of your issue: what do you have at the moment and what do you want to achieve (or how do you see it should be)? At the moment it's hard for me to advice you something or elaborate on this, because I don't feel I understand you clearly. Thanks!

@thorjelly
Copy link

Does T implement Send and Sync? If so, you may wrap T in web::Data::new() when you add the data and it will not internally wrap it in Arc. If it does not implement Send and Sync, you can wrap it in a mutex. The doc for web::Data has an example doing exactly that.

@jeff-hiner
Copy link

jeff-hiner commented Jan 30, 2020

I'm having a similar issue right now. Basically I'm trying to use actix-web as a small piece of a larger application to handle REST calls that reflect state that's already stored as Arc<Mutex<FooState>>.

web::Data is a newtype struct that internally stores Arc<T>, so it feels like there should be some way to create a web::Data<T> from an Arc<T> (I'd expect it to invoke clone). But the only way to create web::Data is via new, which invokes Arc::new. I don't see any evidence that the code skips double wrapping Arc:

https://docs.rs/actix-web/2.0.0/src/actix_web/data.rs.html#74

So if I call web::Data::new(someArc<T>) internally I wind up with Arc<Arc<T>>. Do you see the problem?

@cdbattags
Copy link
Member

cdbattags commented Jan 31, 2020

@jeff-at-dwelo latest Rust 1.41 that dropped today with the loosened orphaned traits rules means you should be able to fix this quick directly inline in your project and then feel free to PR your solution

See https://rust-lang.github.io/rfcs/2451-re-rebalancing-coherence.html

@SOF3
Copy link
Author

SOF3 commented Mar 18, 2020

@thorjelly the point here is that the Arc was created by something else beyond my control (think a library that returns Arc<T>). I have no chance to move T out of the Arc.

@svenallers
Copy link
Contributor

I am currently facing a similar issue.

My motivation:
I want to share commonly used data structures like database connection in a neutral fashion, which is independent of any library, as they could also be needed for different endpoints that are not provided by actix (e.g. event streams).

I would be more than happy to contribute a solution as this should not be all to hard to implement if you are fine with it.

@robjtede robjtede added C-improvement Category: an improvement to existing functionality and removed question labels May 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-improvement Category: an improvement to existing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants