Skip to content

How is mutability for headers implemented in the Rhai plugin system? #2632

Answered by SimonSapin
martindisch asked this question in Q&A
Discussion options

You must be logged in to vote

I had trouble understanding this at first, too. What makes this work is a perhaps-surprising feature of Rhai called chaining updates. In this Rhai code:

request.headers["example"]  = "example";

… what happens in terms of Rust code is:

  • The getter for headers on subgraph::Request is called, returning a cloned HeaderMap
  • The indexer-setter for HeaderMap is called with a key and new value. This modifies the cloned map but not (yet) the request.
  • Then because the above was a chaining update, Rhai implicitly calls the setter for headers on subgraph::Request with the mutated HeaderMap. The bindings for that one are:
            $engine.register_set(
                "headers",
                |obj:

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@martindisch
Comment options

@garypen
Comment options

@martindisch
Comment options

Answer selected by martindisch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants