xrust: Using RNode in async code doesn't work because of Rc
I am writing a project that uses xrust at https://git.sr.ht/~latex/streamlist I’m trying to use xrust in async code, and it’s not working. How would I get this to work?
error: future cannot be sent between threads safely
--> src/serve.rs:80:9
|
80 | / Box::pin(async move {
81 | | let response: Response = future.await?;
82 | | let stylesheet = RNode::new_document();
83 | | let body = response.into_body();
... |
188 | | }
189 | | })
| |_____error: future cannot be sent between threads safely
--> src/serve.rs:80:9
|
80 | / Box::pin(async move {
81 | | let response: Response = future.await?;
82 | | let stylesheet = RNode::new_document();
83 | | let body = response.into_body();
... |
188 | | }
189 | | })
| |__________^ future created by async block is not `Send`
|
= help: within `{async block@src/serve.rs:80:18: 189:10}`, the trait `Send` is not implemented for `Rc<xrust::trees::smite::Node>`, whic
h is required by `{async block@src/serve.rs:80:18: 189:10}: Send`
note: future is not `Send` as this value is used across an await
--> src/serve.rs:84:65
|
82 | let stylesheet = RNode::new_document();
| ---------- has type `Rc<xrust::trees::smite::Node>` which is not `Send`
83 | let body = response.into_body();
84 | let bytes = match axum::body::to_bytes(body, 65536).await {
| ^^^^^ await occurs here, with `stylesheet` maybe used later
= note: required for the cast from `Pin<Box<{async block@src/serve.rs:80:18: 189:10}>>` to `Pin<Box<dyn Future<Output = Result<Response<
Body>, <S as Service<Request<Body>>>::Error>> + Send>>`
_____^ future created by async block is not `Send`
|
= help: within `{async block@src/serve.rs:80:18: 189:10}`, the trait `Send` is not implemented for `Rc<xrust::trees::smite::Node>`, whic
h is required by `{async block@src/serve.rs:80:18: 189:10}: Send`
note: future is not `Send` as this value is used across an await
--> src/serve.rs:84:65
|
82 | let stylesheet = RNode::new_document();
| ---------- has type `Rc<xrust::trees::smite::Node>` which is not `Send`
83 | let body = response.into_body();
84 | let bytes = match axum::body::to_bytes(body, 65536).await {
| ^^^^^ await occurs here, with `stylesheet` maybe used later
= note: required for the cast from `Pin<Box<{async block@src/serve.rs:80:18: 189:10}>>` to `Pin<Box<dyn Future<Output = Result<Response<
Body>, <S as Service<Request<Body>>>::Error>> + Send>>`
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 16 (13 by maintainers)
Good news! I have managed to implement a new version of QualifiedName that should be able to work with an Arc-based interner and is compatible with the Transform module. See branch intern-exp-2. I’ve designed this QName to not leak memory and be fully self-contained so Display and Debug will work.
I’m now going to try substituting the old QName for the new one and see how that goes.
I’ve started the implementation to use slotmap for interning. I chose slotmap because it also supports being able to setup a mapping to the QName components, using SecondaryMap, because the transform needs to be able to retrieve the namespace URI, local name, and prefix components.
The implementation will touch pretty much every module in the codebase, so it’s going to take some time to get it all done. I’ll probably temporarily cut out most of the modules to get a proof-of-concept before finishing the entire job. Accordingly, this is going to take a while…
Happy New Year @mickdekkers !
The reason Rc has been introduced into QualifiedName is as a first step towards string interning. So I would focus on finding a solution to interning strings that is Send and Sync compatible. The reason I haven’t gone straight to full string interning is that I don’t want to introduce something that leaks memory. So let’s start looking for a solution that meets all of these requirements.
Your first point is entirely the point of interning strings. The primary use case is transforming a source document using a stylesheet document. However, once compiled into a QualifiedName the interned name should be independent of the document that it originated from. Also, string interning allows name comparison to be performed by comparing pointers, rather than the string value. This will result in a significant performance improvement.
I believe the last point already exists. QualifiedName::new() and QualifiedName::new_from_values() allow the construction of a QName from either String or Value respectively. There’s also the TryFrom methods. Things get tricky when a prefix needs to be resolved, but the use of NamespaceMap fixes that since it is also independent of a Document. There is a convenience TryFrom method that is intended to be used when a Document does exist, to resolve namespace prefixes, but apart from that the qname module does not rely on Node.
The solution should also handle Value, rather than String. I’m starting to think about support for data types in XPath 2.0+ so would want to support dates and times, etc. There are a lot of values that have a distinct string representation, but the same normalised value, e.g. 1.0 and 1.00 or 2 and 002.