News for hackers. Stuff that matters.
Re:Exploring System76's New Rust Based Desktop Environment by Narishma on Thursday January 13, 2022 @03:00PM (#29926000)
GTK is just a library, you can use it from any language it has bindings for.
Re:Exploring System76's New Rust Based Desktop Environment by diskzero on Thursday January 13, 2022 @03:20PM (#29926262)
Understood. I have made significant contributions to GTK. My contributions possibly had memory leaks or memory corruption issues that will bring down the higher Rust layer. I am trying to understand the purpose of the Rust layer. It is fine with me if it is because Rust is interesting, but what is being presented doesn’t seem like a Rust desktop environment _to me_.
Re:Exploring System76's New Rust Based Desktop Environment by Taywee on Thursday January 13, 2022 @03:45PM (#29926586)
The big selling point of Rust in respect to these kinds of scenarios is that it's very often that a C or C++ library says things like "You must not call function CallAfterFoo before function Foo is called", or "Once you call DestroyObject, that object must not be used again", or "You must not call SomethingDangerous while a ResourceOwningObject exists", and so on and so on.

In well-tested libraries like GTK, SQLite, Curl, and such, they are often quite robust just based on having been heavily developed and tested by many people over a very long time, and there are still ways that they can be misused and abused that are usually well-documented and warned against. A well-developed Rust wrapper actually makes it impossible to misuse one of these libraries from Rust, and therefore better enables a much smaller team of developers to write secure, robust applications. Rust can guarantee these documented restrictions at the type level and even make impossible many error conditions.

So even though the UI is GTK, Rust still enables developers to write more robust applications with less fear. Personally, I find that GTK with Rust is a very pleasant experience. It's less about guaranteeing that the lower libraries have no bugs and more about preventing people from interacting with the libraries in dangerous or buggy ways.

Re:Exploring System76's New Rust Based Desktop Environment by ranfdev on Thursday January 13, 2022 @03:52PM (#29926677)
I've made some small contributions to gtk-rs-core (the library providing rust bindings to glib, gdk...).

While the lower layers written in C do impact the overall safety, the bindings are made to be as safe as possible.

For example: every glib parameter that may take NULL in Rust becomes an Option<T>.

GObject's methods are defined on traits and checked by the Rust type system. There are also some macros to provide an easy and safe interface to the GLib type system.

All of this directly applies to gtk-rs.

Overall, the bindings are well documented and with many examples. There's even a book. Also, there's a great community around them.

Bindings website: https://gtk-rs.org/

Re:Exploring System76's New Rust Based Desktop Environment by IshKebab on Thursday January 13, 2022 @03:30PM (#29926412)
It's not 100% Rust but I think you would expect that GTK itself is fairly well tested - probably much better tested than any new app you write that uses it, so it is still worth it to write that app in Rust.

And it has to be said that memory safety isn't Rust's only compelling feature. It also has a pretty great build system, a decent library ecosystem, a very strong type system which gives you an "if it compiles it works" experience surprisingly often, probably the best multithreading system, etc. etc.

Re:Exploring System76's New Rust Based Desktop Environment by pxc on Thursday January 13, 2022 @05:09PM (#29927625)
I agree! I think they're just using Rust because they'd rather use it than C, not because they need safety guarantees for the DE applications.
Re:Exploring System76's New Rust Based Desktop Environment by jandrese on Thursday January 13, 2022 @03:31PM (#29926420)
The issue is that Rust is extremely opinionated at to how code should be structured and there is going to be considerable interface friction with a system that is built around aliasing data every time there is a callback.
Re:Exploring System76's New Rust Based Desktop Environment by acdha on Thursday January 13, 2022 @03:41PM (#29926541)
Do you have an example of that friction? Looking through the docs at e.g. https://gtk-rs.org/gtk4-rs/stable/latest/book/hello_world.ht... it doesn't seem that bad but not having used it I would be curious how it works out once you're building a serious app.
Re:Exploring System76's New Rust Based Desktop Environment by jandrese on Thursday January 13, 2022 @03:56PM (#29926724)
Things are already getting quite hairy by page 4[1] of that link. And that example is showing data completely owned by the GObject. If you need to pass a mutable reference to third party data to a GObject it's not going to work in Rust. Imagine a button that you click to change the contents a GtkTreeModel connected to a GtkTreeView--you're going to need to work hard. You're going to write a fair bit Rust specific glue code to work around these issues.

[1] https://gtk-rs.org/gtk4-rs/stable/latest/book/gobject_memory...

Re:Exploring System76's New Rust Based Desktop Environment by mmstick76 on Thursday January 13, 2022 @08:45PM (#29929618)
You can avoid virtually all friction by sidestepping shared mutable references altogether and writing your GTK application in a more idomatic Rust way. For example, instead of passing shared references of application state to every widget signal, you can pass async Rust channels to signals. You can spawn a single async event loop attached to GLib's main context that listens for events sent from those signals, and manage all your widgets and application state in one place.

There's some libraries that effectively automate GTK in this way, such as relm4.

Re:Exploring System76's New Rust Based Desktop Environment by rollcat on Thursday January 13, 2022 @04:53PM (#29927402)
Asking as someone who's never spent significant time/effort working with Gtk/GObject in C (only a bit in Python). Isn't it still generally desirable to have the compiler yell at you, if you can't convince it you know what you're doing? Rather than allowing the possibility of memory corruption.

I do agree that the code in the example is far from beautiful. I wonder if we were to redesign GObject from scratch, if we could make interfacing with Python, Rust, JS, etc a bit less hairy.

Re:Exploring System76's New Rust Based Desktop Environment by Someone on Thursday January 13, 2022 @06:13PM (#29928344)
The work you have to do to show your compiler that certain constraints hold (say that i and j point inside the same array and that i < j can be extremely cumbersome, even in cases where it’s obvious that they do hold. That makes it not seem worth the trouble.

Of course, that obvious may turn out to be incorrect for some edge condition.

Basically, that’s the same reason why mathematicians don’t put all proofs through a proof assistant.

Re:Exploring System76's New Rust Based Desktop Environment by tialaramex on Thursday January 13, 2022 @08:26PM (#29929456)
Of course, to the extent you assumed things the machine didn't test that.

The core of TLS 1.3 was proved before it shipped. But, the proof makes an assumption which has consequences. It assumes when communicating using an agreed shared secret† you have a separate shared secret for every such pairing. So Alice and Bob need a shared secret but (and this is where humans trip up compared to what was actually proved) the proof says Bob and Alice also need a shared secret different from the one for Alice and Bob.

The consequence of this accidentally missed assumption is the Selfie attack. Alice and Bob share a secret S and communicate over the Network using TLS 1.3. Bob fed the cat half an hour ago. The cat has employed Mallory to trick Alice into feeding it even though Bob already did. Alice sends a message on the Network. It is encrypted with S and it says "Did you feed the cat?". Mallory doesn't know S and can't read this message or tamper with it. But Mallory simply redirects the message back to Alice. Alice receives a message, properly encrypted using S, which says "Did you feed the cat?". She presumes this message is from Bob, so she answers "No, go ahead and feed the cat". Mallory redirects this response back to Alice too. Alice receives "No, go ahead and feed the cat" and she concludes Bob hasn't fed the cat so she feeds it again.

Oops.

The proof was fine, but we brought an assumption along that we did not clearly articulate.

† You never use this mode in your web browser, but IoT things might do this because it's easier than all that stuff with certificates.

Re:Exploring System76's New Rust Based Desktop Environment by southerntofu on Friday January 14, 2022 @07:39AM (#29933741)
> The work you have to do to show your compiler that certain constraints hold

In rust, that's what the unsafe keyword is allowed. It doesn't mean that the code is actually unsafe, but rather that the compiler should trust you on this one.

Re:Exploring System76's New Rust Based Desktop Environment by pjmlp on Friday January 14, 2022 @01:51AM (#29931343)
Thanks for the link, the last time I used Gtk-rs, the book wasn't yet a thing, you can imagine how much fun I had learning those patterns on my own.

Yes, there is relm nowadays, which was yet to be born when I did my Gtkmm => Gtk-rs port.

At least I can also now point others to the book.