last day (15 days later) » 

7:49 AM
4
A: How do I extract Ref<a,b,c> from Option<Ref<a,b,c>> in Rust

cdhowieThe dashmap Ref and RefMut types are so-called "guards." They implement the Deref* traits as appropriate, which allows them to be used a lot like a reference in most cases. The reason guards exist is usually so that they can implement Drop, which allows them to do something when you're finished...

 
this worked! Thanks for the detailed explanation. My goal is to implement 1 struct for all and make development easier but now it seems more complicated than i had intended.
 
@user2066671 Indeed, it can be complicated. What we're doing here is making returned references polymorphic, but unlike a lot of other languages, we have to implement the polymorphism ourselves. There is a way that you can let the language implement the polymorphism for you, but this requires boxing. Basically you'd return a Option<Box<dyn Deref<Target=V> + '_>> and box the reference or the dashmap guard. If you'd like an example of that approach I can add one to my answer.
 
Thank you, i would love to see an example. Would there be any drawbacks by using boxing? My goal is to expose the high performant datastructures without compromise
 
@user2066671 Both approaches have trade-offs. The approach I give in my answer is probably more performant than boxing because boxing adds a level of indirection and a heap allocation. On the other hand, boxing requires less code to implement. (Both require the same amount of code to use so if performance is your concern then the enum approach is better.)
@user2066671 I added an example. You basically use a dynamic Deref* as the guard type. This works because both &V and dashmap's Ref implement Deref, and ditto for &mut V and dashmap's RefMut implementing DerefMut.
@user2066671 Another approach would be to instead create a map-like trait and implement it on both types of maps. Rust allows you to implement your own traits on foreign types, which is something none of the most popular languages really let you do.
@user2066671 I've added an example of the trait approach.
 
Thank you for the posting the trait examples, that seems the cleanest. I do have some questions.
 
7:52 AM
Feel free to ask and I will try my best to answer.
 
so lets say i want to abstract another custom hashtable, it would return Result<_, Err> instead of the Option<&v> for insert. Do I have to adjust the base trait to accomodate both?
 
That's one option. You could also discard the error by calling the .ok() method on the result, which turns Ok(v) into Some(v), and Err(e) into None.
So, for example, you could have

trait HashTable {
// ...

type FetchError;

// ...

fn get<'a>(&'a self, key: &Self::Key) -> Result<Self::Ref<'a>, Self::FetchError>;
}
 
i see, i'm thinking about wrapping the base trait and all the other datastructures in a Result enum so everything is uniform
 
Then for the error-less hashtables you would have type FetchError = (); which you can map to by calling .ok_or(()) on the options.
Or you could create a struct MissingKeyError; and use that for the error-less types.
Though I have to wonder what other possible error you could have than "the key isn't present."
 
how would one handle different number of parameters in the case between hashtable1 and hashtable2, one insert takes (k,v), another takes insert(k,v, guard=abc), should I set that on the trait with an optional parameter? something like insert(k,v, guard=None)?
 
8:03 AM
That's where you have to start making decisions about what the trait will support, because if each implementation has its own special cases about what an insert call looks like, then the trait becomes useless, as users need to know what kind of table they are using so they can supply the correct parameters.
Abstractions necessarily lose some detail.
 
agreed
 

last day (15 days later) »