Avatar

KillTheMule

KillTheMule@programming.dev
Joined
1 posts • 9 comments
Direct message

Hmm, right. I think it still might be warranted in niche cases, but trying to think of such a case made it pretty protracted in my head… maybe when functions can also be called for side effects, and the into conversion is costly and the caller might not care about the return value?

permalink
report
parent
reply

2 was good, thanks. 4 needs a tad more thought imho, returning an impl T does have its place, because it makes changing the return type of the function a non-breaking change.

permalink
report
reply

Non-tutorial suggestion: I’ve you’re stuck, put a demonstration of your problem on the rust playground, post it here with the question. People in rustland are generally very willing to help out, and the playground is a very helpfull tool for that.

permalink
report
reply

I don’t know, I was just surprised by the short timeframe.

permalink
report
parent
reply

This parting shot sounds pretty dire

a bug in safe code can easily cause unsound behavior in your unsafe code if you’re not careful.

That’s definitely not how it should be. Fortunately, I think I disagree with that, since miri points to the “real” buggy code:

unsafe { inner.as_ref() }

As opposed to the article, I’d argue this code is not correct, since it did not account for alignment, which it must (I mean, by standard use of the word unsound this is unsound, since it can be called from safe code introducing UB). Or am I wrong? Is the fundamental value proposition of rust moot?

permalink
report
reply

A reference IS Copy, by the simple fact that it is a primitive value on the stack.

This seems a bit misleading, noting that unique/mutable references aren’t Copy. Shared references are Copy because it’s sound to have that, and it’s a huge QOL improvement over the alternative.

permalink
report
parent
reply

Wow, they’re sort-of-targeting edition 2024. I did not expect this, holding my breath ;)

permalink
report
reply

In fact, isn’t this not true just by the fact that references work for Strings and Strings size can’t be known at compile time?

I don’t understand this. Shared references to String are Copy, too. This doesn’t have to do anything with sizes. Rather, it’s implemented in the compiler, because it’s sound to have it and a huge QoL improvement over the alternative… just the same reason why e.g. usize is Copy, really.

is it dereferenced specifically because is Boxed on the heap?

No, it’s not really related to the heap. Box implements DerefMut, which is in-depth explained here.

permalink
report
reply

While funny, this also highlights part of why I like rust’s error handling story so much: You can really just read the happy path and understand what’s going on. The error handling takes up minimal space, yet with one glance you can see that errors are all handled (bubbled up in this case). The usual caveats still apply, of course ;)

permalink
report
reply