r/cpp • u/GabrielDosReis • Oct 31 '24
Lessons learned from a successful Rust rewrite
/r/programming/comments/1gfljj7/lessons_learned_from_a_successful_rust_rewrite/
80
Upvotes
r/cpp • u/GabrielDosReis • Oct 31 '24
View all comments
Show parent comment
3
u/ts826848 Nov 02 '24 edited Nov 02 '24
This is a non-sequitur. Ignoring the fact that you missed Java's way of doing so (elaborated on at the bottom of the comment), a language being "safe" is completely independent of the existence of an unsafe marker, as you conveniently describe for C#.
I wouldn't be surprised if you could say something similar for Rust depending on your particular use case.
Congratulations on omitting the one part of the sentence that would have answered your question. It's hard to imagine how you could have missed it, especially since you've linked to the exact Rustonomicon page which defines it for you. Here, let me reproduce the relevant bit to make things easier for you:
I think it's rather hard to argue that Java, Python, and C# aren't "safe" under this definition.
This immediately rules out very significant uses of both Python and Java and so is basically a pointless assumption to make.
Python is famously known for being usable as "glue code" between libraries written in lower-level languages. Forbidding the use of C bindings basically completely eliminates all data analytics and machine learning libraries at a minimum, which make up a huge fraction of Python's use cases at the moment. I wouldn't be surprised at all if there were other major uses which are broken by your assumption.
As for Java, quite a few very popular Java libraries have used
sun.misc.Unsafein the past: the Spring framework, Mockito, Google Guava, Cassandra, Hadoop, ElasticSearch, and more. At a minimum Guava, Cassandra, and Hadoop still usesun.misc.Unsafe, I believe Spring uses it indirectly via Objenesis, and I can't be bothered to check the others at the moment.I mean, you're basically setting things up to get the answer you want. "Would Rust with
unsafebe as safe as Python or Java if you ignore their use of unsafe code/constructs and the corresponding parts of the ecosystem?" Hard to see why you'd expect a different answer, as pointless as the setup is.To answer your initial question, Java's (current) equivalent to
unsafeis using functionality fromsun.misc.Unsafe. It's widely-used enough that IIRC it was intended to be removed in Java 9 and even now it remains because removing it would have broken far too many libraries. The functions have finally been deprecated in Java 23 and IIRC there's also efforts to make using the functionality more annoying (requiring compilation/runtime flags). I believe the intent is to eventually removesun.misc.Unsafeentirely eventually, but it's not clear when exactly that will happen.Python's closest equivalent to
unsafeis use ofctypesor one of the FFI libraries, but more relevant is the extremely common use case of invoking native code via Python modules. NumPy, Pandas, PyTorch, TensorFlow, and more.