I am writing a grammer defined as a bTreeMap, the key is the token and the value is a GrammerDefinition struct. The struct in question stores metadata about a specific token, such as the error name, rules about what is allowed to follow a token and so on. I want to be able to optionally set a callback to handle errors and rules of said metadata. Is this the most optimal pattern and how might this look in code? Any help would be greatly appreciated, I have been googling for around a month.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (10)
I don't know about most optimal, but I've had success using an Rc:
You'd need to adjust the signatures to match your criteria. Construct with, e.g.,
Callback::from(|| -> T {//...})
.Does this seem like it'd fit your usage?
Ben to the rescue again, I will give it a shot (and read about RC) as soon as I'm home. Currently on a crowded train trying to look busy π
My Rust senses were tingling :)
Hey Ben, I am just revisiting this.
How can I access
self.foo
fromis_import_statement
whereself
which comes from the ASTNode struct.Hrm. At a glance, I'm not positive you can nor should be able to. The callback won't be able to access any context not provided at instantiation, you'd need to accept an additional argument like
dyn Fn(&str,&str)
, which is usually how I skirt around this sort of thing in Rust. The callback has no concept of any parent object that owns it. I might not fully understand the context here, though - there always is a way to do what you want but not always with the structure you thought.Do you have a fuller code sample I could play with? Might not get there tonight but can play with it this weekend.
Maybe the callback struct could also contain a
&'a ASTNode
referencing the parent, passed in to the constructor, but it gets messy. You'd probably shift away from theFrom
trait and just provide aCallback::new()
.Oh I made a few tweeks and ended up doing this
so thats kind of what I wanted... not as nice a real relationship between the callback and the struct it lives in.
yup, that was my first thought but I agree, not what you're gunning for. Maybe try the stored reference?
I think I can live with it tbh, it works well. Ultimately I can progress to the next bit of Jess's lexer. This part of the code isn't committed yet, the whole solution is a bit higldy pigldy. Sorry to rubber duck, I find writing about problems is enormously helpful.
I will check out the communities you suggested and such π.
Thank you so much once again.
Don't apologise! I get a lot out of it too, and think you've got a cool project going on.
Also, the /r/learnrust subreddit is small, but generally helpful, and the main /r/rust subreddit may help you too. They're pretty intense, but highly knowledgeable and you may be pushing the edge of what I understand - not that I'm not curious to learn this solution.