[rust-dev] What do I do if I need several &muts into a struct?

Vladimir Pouzanov farcaller at gmail.com
Mon Jun 16 13:59:45 PDT 2014


I have a problem figuring the reasonable data access pattern for my code,
here's a brief description. I have a tree structure where each node has a
path and an optional name. Path is used to locate the node in tree
hierarchy, names are stored in a flat namespace as node aliases:

lpx17xx at mcu {
  clock {
    attr = "value";
  }
}

os {
  thread {
    entry = "start";
    ref = &lpx17xx;
  }
}

this tree has 4 nodes, /mcu, /mcu/clock, /os and /os/thread. /mcu is also
known as "lpx17xx". I use the following structure to hold root nodes:

#[deriving(Show)]
pub struct PlatformTree {
  nodes: HashMap<String, Gc<Node>>,
  named: HashMap<String, Gc<Node>>,
}

Everything works fine up to the point of references. References allow some
nodes to modify other nodes. So, the first pass — I parse that snippet into
PlatformTree/Nodes struct. Second pass — I walk over the tree and for each
reference I invoke some handling code, e.g. in the handler of thread node
it might add some attribute to lpx17xx node. Which is not really possible
as it's in immutable Gc box. Also, such modifications are performed through
`named` hashmap, so I can't even store &muts in `nodes`, as I still can
store only immutable pointers in `named`.

How would you solve this problem?

-- 
Sincerely,
Vladimir "Farcaller" Pouzanov
http://farcaller.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140616/e0f18ae4/attachment.html>


More information about the Rust-dev mailing list