« first day (1370 days earlier)      last day (2123 days later) » 

5:34 AM
Hi @FrancisGagné are you around now to discuss the dupe question merge? IMO it doesn't seem suitable to move your answer to the other (question merge), as you have tailored your answer to Marc Miller's question, and there are comments with him under the answer as well
 
@SamuelLiew yes I'm here
ping @Shepmaster
the plan was to edit the answer to fit the new question, but I get your concern regarding the comments
 
I propose editing the answer such that it can fit either question first
then if the comments are no longer needed, we can delete them
or you should edit the additional info discussed in the comments into the answer
Once everything is settled you can ping me in this room again
question merge is not a reversible action by mods, so we want to be extra careful not to break context
if the context for one question-answer pair is slightly different, then it may be best to keep them marked as dupe and leave it as that
 
it might just be better for me to post a new answer that's tailored to the target question then
 
6:08 AM
0
A: How do I return a reference to something inside a RefCell without breaking encapsulation?

Francis GagnéYou can wrap the Vec in an Rc. use std::cell::RefCell; use std::rc::Rc; struct MutableInterior { hide_me: i32, vec: Rc<Vec<i32>>, } struct Foo { interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> Rc<Vec<i32>> { self.interior.borrow().vec.clone()...

 
6:18 AM
great!
 
 
7 hours later…
1:45 PM
@PeterHall and I've reworded the old question "returning-the-t-borrowed-from-refcellt" is now free
@LukasKalbertodt that &mut 42 Q; dupe of stackoverflow.com/q/47662253/155423 ?
 
2:16 PM
@Shepmaster Dang it, yep, that's a dupe. I searched for duplicates, but couldn't find anything. Strange, considering that I remember reading your answer to that question at least once...
 
@Shepmaster Ok, I'll write another question with a similar title
 
@LukasKalbertodt I mean, it could be different cause of the mut?
 
3:01 PM
@Shepmaster Done.
 
@Shepmaster but the question you linked also talks about mut? Or am I missing sometihng?
 
@LukasKalbertodt haha, truuu
 
 
8 hours later…
10:57 PM
I think I'm going to add a Key::default that's a 'null' key
because I noticed that Option<Key> is very common (and inefficient)
note that 'invalid keys'
e.g. let k = sm.insert(3); sm.remove(k); // k is now invalid
are already a thing, so null keys don't really change anything in terms of more complexity to think about
 
11:28 PM
@orlp if you can make one of the fields of Key a non-zero integer, the niche-filling enum optimization will kick in
 
@trentcl experimental only
and no, I don't think I could do that
 
Hmm... I guess it needs nightly though
yeah
 
the null key would be index == u32::max() - 1
and consequently the container would have one less maximum capacity :P
but I don't think anyone would lose sleep over that ^^
 
@orlp That seems likely to be unpleasant, since you have to special-case the "not quite maximum" value any time you do math on indices... (although maybe it never really comes up)
 
@trentcl huh?
how so?
 
11:38 PM
@orlp You're starting indices at 0 and incrementing as you add stuff, no?
 
@trentcl yes
keep in mind I already have a check to see if index == u32::max() in insert
 
sure
 
to make sure I don't overflow the container
 
checked_add(1)
that doesn't work anymore
 
that just changes, so instead of checked_add I do a regular add followed by an if statement
 
11:41 PM
eh, that works
 
and I don't think it comes up in any other place
 
personally, I'd rather just say indices start at 1 and let 0 be the special case so that enum optimizations can help out
(which doesn't necessarily just affect Option<Key>, but also Result<Key, ()>, or any user-created enum)
 
@trentcl then either I have to waste some space
or do arithmetic everywhere
 
ok, so you waste a slot. VecDeque does that
 
(I'd rather waste space)
but either way
it's still slower
with null keys I don't need to check the enum at all
 
11:44 PM
I'd take the hit to have Option<Key> over BespokeOptionKey
orrrrrr
maybe version could be the non-zero field?
 
@trentcl it could
it already is
in Key
but again, it's still slower
 
@orlp Option<Key> is slower than a default value?
 
why do I feel like I shouldn't be able to write impl Index<MyKey> for HashMap<MyKey, MyValue> { ... } in my crate? std could add an impl<K, V, S> Index<K> for HashMap<K, V, S> { ... } (with the appropriate bounds) that would overlap with my impl, no?
 
@trentcl it depends on the operation
 
@FrancisGagné this confuses me too
 
11:48 PM
@trentcl ok.is_some() and k.is_null() would be the same speed
but
sm[ok.unwrap()] is slower than sm[k]
and both are safe because sm[k] does a bounds check anyway
 
hmm
 
don't get me wrong
I'll still do the nonzero thing anyway for the other optimizations you've mentioned
the question is just whether to introduce a null key :D
 
I don't think I have a problem with the existence of a null key
but I guess I can't think of an API where I would want to use one
 
@trentcl compare these two:
eh the latter doesn't compile because I forgot some selfs
 
first one is broken, it's an edit link
 
11:58 PM
but the idea is clear
my bad
fixed
 
thx
 

« first day (1370 days earlier)      last day (2123 days later) »