Closed thlorenz closed 2 years ago
@chertov could you please verify that this addresses your needs (you mentioned here https://github.com/thlorenz/rid/issues/5)?
Which key/val
types are most important for you?
I'm planning to focus on the following two for now HashMap<String, String>
, HashMap<String, struct>
and hash maps with primitive keys/values.
Summary
This PR adds the ability to export a reference to a hash map field. So far only
HashMap<u8, u8>
has been tested in order to have the generals worked out. More types will be added later as this PR is now large enough :)First Use of Recursive rid Macro Expansion
In order to allow iterating the keys of the hash_map we use the first case of applying
[rid::export]
recursively. Thus this access is simply implemented by emitting the following:I first tried a different route (visible from some of the commits) to fully render the method returning a
RidVec
and rendering the access toRidVec
s via nested accesses.However the final solution turned out to be so much simpler and basically is the first example of using rid features we can already render like a Lego piece in order to render higher level types and implementations. I plan to use this approach in more cases going forward.
Standalone Export as part of this PR
However to make this happen I had to implement standalone
[rid::export]
s. Up til now only exports that were part of animpl
were allowed since I couldn't think of a clean way to prevent arid::export
to be rendered twice (once as part of the impl and once treated separately).I now track exports that we rendered as an impl method and skip rendering it again, see 631701d.
Potential Future Issues
This works, but would hide a user error in which a method/function with the same name is once exported as an impl and then again as a separate method, i.e. it doesn't raise an error, but just ignores the second one, as in this example:
If this causes lots of issues (which I don't really doubt) we'll have to take the tokens of the function into account in order to alert the user when two functions have a name colission.
Follow Up PR
As mentioned above this PR will be followed up with one that adds support for more HashMap
key/val
types.Related Future Features
After that the following related features are on my radar.
HashSet
,BTreeMap
andBTreeSet
(shouldn't be too difficult as they are very similar toHashMap
Vec
s viaRidVec
However before I'll implement another sample app, this time to show off and battle test exporting
HashMap
s.Commits