Actix-web: impl From<Arc<T>> for web::Data<T>

Created on 21 Jan 2020  路  6Comments  路  Source: actix/actix-web

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.

C-improvement

Most helpful comment

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.

All 6 comments

@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!

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.

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?

@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

@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.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

volfco picture volfco  路  4Comments

cheolgyu picture cheolgyu  路  3Comments

joshsleeper picture joshsleeper  路  3Comments

bbigras picture bbigras  路  5Comments

Dadibom picture Dadibom  路  4Comments