News for hackers. Stuff that matters.
Re:Building native macOS applications with Rust by amelius on Wednesday November 30, 2016 @10:45AM (#13071616)
How easy is it to write closures for event handling in Rust?

Is it anywhere near as easy as doing this in Javascript? Or does it require such things as manual memory management?

Re:Building native macOS applications with Rust by pcwalton on Wednesday November 30, 2016 @11:12AM (#13071878)
Here's an example from gtk-rs: https://github.com/gtk-rs/examples/blob/master/src/text_view...

(See other files in that repo for more examples.)

It's not quite as low-friction as JS, but it's awfully close.

Re:Building native macOS applications with Rust by careersuicide on Wednesday November 30, 2016 @12:05PM (#13072430)
It's not JavaScript easy, but it's not too hard once you wrap your head around Cell, RefCell, Rc, and friends.

Here's an example using GTK that does something vaguely useful, I have no idea if it's considered idiomatic, but it works as intended: https://github.com/seaneshbaugh/image_viewer/blob/master/src...

The closures themselves are trivial, it's manipulating the stuff outside the closure that's tricky (for a Rust neophyte like me).

The relevant definitions are

  let area = Rc::new(RefCell::new(DrawingArea::new()));
and

  let scale = Rc::new(Cell::new(1.0));
Rather than just define things as usual they need to be wrapped in a Cell (for value types) or RefCell (for reference types) and then wrapped in an Rc so we can keep track of reference counts at run time.

  {
      let scale = scale.clone();

      let area = area.clone();

      window.connect_key_release_event(move |_, key_event| {
          let mut s = scale.get();

          let key = key_event.get_keyval() as i32;

          if key == gdk_sys::GDK_KEY_plus {
              s += 0.1;
          } else {
              if key == gdk_sys::GDK_KEY_minus {
                  s -= 0.1;
              }
          }

          scale.set(s);

          area.borrow().queue_draw();

          Inhibit(false)
      });
  }
Inside the new scope (which is very important) scale and area are cloned which increments their count, then inside the close scale.get() is used to get a copy of scale (which is possible since it's a float) and then after doing stuff to it based on the key that is pressed it's set using scale.set(). For area it's a bit trickier, it has to first be borrow()'ed before anything can be done with it.
Re:Building native macOS applications with Rust by legulere on Wednesday November 30, 2016 @12:34PM (#13072717)
A bit of bikeshedding on the code you linked:

Why are you commenting out code? If you delete it, it will still be in your git history. If you have some reason to leave it in, document why and wrap it with a `if false {}` or `if debug {}` instead. That way autoformatters will not destroy your code and the compiler makes sure your code keeps compiling.

Leaving a line empty between statements helps structuring code. Putting an empty line after each statement makes the code as unstructured as without.

Re:Building native macOS applications with Rust by Frondo on Wednesday November 30, 2016 @01:51PM (#13073495)
My personal reason for commenting out code for a few revisions, instead of deleting right away, is to keep a reminder of the work in progress. When I'm satisfied that the old code's really done for, I delete it and leave it to the history.

Until then, it's easier to review the commented-out bits than to go back through version history. (And plenty easy to delete it when it's ready to be deleted!)

Re:Building native macOS applications with Rust by problems on Wednesday November 30, 2016 @02:32PM (#13073867)
Your VCS should really make it quite easy to go back through version history. If it's not doing that, that's a separate problem.

Maybe it's just me, but I find it an eyesore to have large blobs of commented out code laying around even for a short period (not to mention the odds of me remembering to delete them are close to nil).

Re:Building native macOS applications with Rust by kough on Wednesday November 30, 2016 @04:25PM (#13074830)
How do you remember which commit to diff against? How do you easily pick out the relevant parts of that diff once many changes have occurred - months or even years later?
Re:Building native macOS applications with Rust by saghm on Wednesday November 30, 2016 @07:43PM (#13076054)
> How do you remember which commit to diff against?

Ideally your commit message would indicate this. If you're always consistent in the language you use to describe similar changes, you can just search through your git log to find it.

> How do you easily pick out the relevant parts of that diff once many changes have occurred - months or even years later?

You can use `git show` to just look at that one commit where you deleted the code

Re:Building native macOS applications with Rust by problems on Wednesday November 30, 2016 @02:30PM (#13073851)
Seems fairly comparable to the way it's done in C++11 lambdas.
Re:Building native macOS applications with Rust by steveklabnik on Wednesday November 30, 2016 @02:37PM (#13073906)
C++ lambdas and Rust closures are very similar as far as I know, the only major difference being that they use explicit capture lists and we don't.
Re:Building native macOS applications with Rust by JoshTriplett on Wednesday November 30, 2016 @04:17PM (#13074768)
On the other hand, Rust closures do compute the capture list, and then let the type system reason about the safety of those captures, which C++ lambdas (or blocks with the clang blocks extension) don't.
Re:Building native macOS applications with Rust by problems on Wednesday November 30, 2016 @04:47PM (#13075018)
Ah okay, so rust will only ever capture the minimal set of what's necessarily used in the lambda? Different from using [&] or similar in C++ where you capture everything.
Re:Building native macOS applications with Rust by problems on Wednesday November 30, 2016 @02:44PM (#13073973)
Yeah, in C++ you can sort of get away without explicit capture of all variables too by doing [=] or [&]. Though I suppose it's still explicit in a way, just saves the need to list everything.
Re:Building native macOS applications with Rust by pavlov on Wednesday November 30, 2016 @10:49AM (#13071647)
Rust doesn't have manual memory management -- it just requires you to be very explicit so that the compiler can manage memory for you ;)
Re:Building native macOS applications with Rust by amelius on Wednesday November 30, 2016 @11:08AM (#13071840)
Being very explicit is what I call manual :)
Re:Building native macOS applications with Rust by kbenson on Wednesday November 30, 2016 @12:08PM (#13072461)
I would say there's still a pretty big difference between using malloc to allocate space for an array of structs which you have to remember to free later and letting variable definitions and scopes handle that for you, like in many managed languages.
Re:Building native macOS applications with Rust by digikata on Wednesday November 30, 2016 @12:17PM (#13072555)
I would call memory in Rust manually managed, but automatically accounted. Vs C which would be both manually managed and accounted.
Re:Building native macOS applications with Rust by kaoD on Wednesday November 30, 2016 @01:10PM (#13073075)
I'd say the other way around: Rust is manually accounted (you have to take it into consideration when coding) while the compiler automatically manages it for you.
Re:Building native macOS applications with Rust by digikata on Wednesday November 30, 2016 @01:28PM (#13073265)
I think we agree on the fundamental concept, but have a varying way of how one interprets the 'accounting' term: is it the tracking of the memory in various categories (which I think we agree that Rust does automatically in a way that few other languages do), or is it marking of given chunk of memory to be tracked in a certain categoric way (which is what the Rust programmer decides in code). Is there a less ambiguous set of terms to apply for these cases?
Re:Building native macOS applications with Rust by vertex-four on Wednesday November 30, 2016 @11:09AM (#13071847)
The usual way to do event handling in Rust is to either expose an event loop directly, or use an async toolkit where you have multiple components, each owning their own subset of data and processing events related to it, talking to each other over channels, futures, or some other abstraction.

Closures are difficult to use for this due to Rust's concept of values having one owner, making it fiddly to share values over multiple closures without e.g. wrapping them in Rc<RefCell<T>>.

Re:Building native macOS applications with Rust by pcwalton on Wednesday November 30, 2016 @01:00PM (#13072977)
> Closures are difficult to use for this due to Rust's concept of values having one owner, making it fiddly to share values over multiple closures without e.g. wrapping them in Rc<RefCell<T>>.

But note that most UI toolkits already effectively expose all their objects as reference-counted aliasable-mutable—the same as Rc<RefCell<T>>, but more ergonomic.

I wonder if the right solution for this for these frameworks to allow programmers to easily create their own classes in the native widget class hierarchy (COM, Objective-C Foundation, GObject, QObject, etc.), with some combination of macros and other boilerplate generation. Then the rule would effectively be "instead of using Rc<RefCell<T>>, create a native widget class to hold your data if you need to multiply close over it". Not only would this be more ergonomic than the pure-Rust solution, but it'd also interoperate better with the underlying widget system: for example, on macOS and iOS Objective-C introspection APIs would work with such objects.

Re:Building native macOS applications with Rust by vertex-four on Wednesday November 30, 2016 @03:12PM (#13074265)
The issue is less the UI toolkit data and more all the other data in your app.
Re:Building native macOS applications with Rust by pcwalton on Wednesday November 30, 2016 @05:58PM (#13075480)
The solution I proposed is for all that other data.
Re:Building native macOS applications with Rust by k__ on Wednesday November 30, 2016 @10:50AM (#13071655)
https://doc.rust-lang.org/book/closures.html#closures-and-th...

Doesn't seem too crazy, as long as you don't want to mutate the variables the function closed over.

Re:Building native macOS applications with Rust by Manishearth on Wednesday November 30, 2016 @11:54AM (#13072305)
Mutating isn't hard either, and if it does get hard you can stick things in a Cell or RefCell.
Re:Building native macOS applications with Rust by k__ on Wednesday November 30, 2016 @12:04PM (#13072412)
It' not hard, it's just a bit more code than in JavaScript :)