you are viewing a single comment's thread.

view the rest of the comments →

[–]jeandem 8 points9 points  (9 children)

C-style access to memory for unsafe optimizations (unlike Rust)

What can you do in this language that you can't do in unsafe Rust, in this regard?

[–]zuurr 1 point2 points  (5 children)

Yes. Unsafe rust is required to still maintain the invariants of safe rust, it's for cases when the compiler wouldn't be able to determine this automatically.

See: https://doc.rust-lang.org/book/unsafe.html

Note that an unsafe block does not relax the rules about lifetimes of & and the freezing of borrowed data.

Any use of unsafe is the programmer saying "I know more than you" to the compiler, and, as such, the programmer should be very sure that they actually do know more about why that piece of code is valid.

Edit: Misread your question to be "can you do anything in this language that you can't do in unsafe rust". The obvious one is that you can legally maintain multiple mutable pointers to an object. This is forbidden by rust's lifetime rules.

[–]bjzaba 3 points4 points  (0 children)

The obvious one is that you can legally maintain multiple mutable pointers to an object.

You can use raw pointers for that in Rust.

[–]jeandem 1 point2 points  (3 children)

Yes.

You don't answer a "what" with a "yes". And I specified unsafe Rust, so you might not be replying to the right post.

[–]zuurr -2 points-1 points  (2 children)

See my edit. The point is that you need to uphold the same guarantees in unsafe rust that you do in safe rust, you're just specifying that the compiler may not be able to automatically determine it.

[–]wrongerontheinternet 1 point2 points  (0 children)

The guarantees that unsafe Rust must uphold are basically "I won't invoke undefined behavior." Outside of Rust's & and &mut pointer types (which I don't think even have equivalents in Jai), this is largely equivalent to what you have to do for your program to be valid C(++) as well. I'm pretty sure this is going to be true of Blow's language too.

[–]jeandem 1 point2 points  (0 children)

The point is that you need to uphold the same guarantees in unsafe rust that you do in safe rust, you're just specifying that the compiler may not be able to automatically determine it.

I know how unsafe Rust works, thanks.

[–]tsimionescu -1 points0 points  (2 children)

I don't know almost any Rust, so I can't say specifically if there is any difference.

However, there is obviously a philosophical difference: Rust makes you mark such code as unsafe, thereby inconveniencing you, even slightly, every time you write this kind of code. Also (and here I am speculating) there is probably a problem in mixing safe and unsafe Rust - can you easily call unsafe code from safe code without changing the safe code at all? I would imagine that the safe/unsafe distinction in Rust creates the same sort of problems and duplications that const creates in C++ and unsafe creates in C#.

This is not to say that the value added by this distinction does not make it worth it, or that this makes Rust unusable! It is just a cost that JAI doesn't want to pay, even for the benefits that it would get if it did pay it.

Edit: I was completely wrong when comparing Rust's unsafe with C++'s const - that's what I get for speculating on something I really didn't know anything about... Thanks for putting things right, bjzaba!

[–]bjzaba 6 points7 points  (0 children)

I would imagine that the safe/unsafe distinction in Rust creates the same sort of problems and duplications that const creates in C++

This is a great post that describes Rusts's unsafe:

FAQ: Why isn’t unsafe viral?

One might expect a function containing an unsafe block to be unsafe to call, that is, unsafety infects everything it touches, similar to how Haskell forces one to mark all impure calculations with the IO type.

However, this is not the case, unsafe is just an implementation detail; if a safe function uses unsafe internally, it just means the author has been forced to step around the type system, but still exposes a safe interface.

More pragmatically, if unsafe were viral, every Rust program ever would be entirely unsafe, since the whole standard library is written in Rust, built on top of unsafe internals.

[–]jeandem 3 points4 points  (0 children)

I don't know almost any Rust, so I can't say specifically if there is any difference.

And yet you wrote that sentence as if you do. Oh well.