• 1 Post
  • 9 Comments
Joined 10 months ago
cake
Cake day: September 2nd, 2023

help-circle
  • 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?



  • 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.




  • 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 ;)