News for hackers. Stuff that matters.
Re:“I just started writing some Rust” by SamReidHughes on Saturday January 26, 2019 @08:57PM (#19009929)
Maybe I'd call Rust a "resource-constrained" language than low-level. The usecase being you want no GC, for memory inflation or CPU pauses, but suffer a bit in overall CPU time and memory locality.
Re:“I just started writing some Rust” by wtetzner on Sunday January 27, 2019 @10:32AM (#19012022)
Why do you think you'd suffer in CPU time and memory locality?
Re:“I just started writing some Rust” by SamReidHughes on Sunday January 27, 2019 @05:05PM (#19014046)
Sometimes you have to put stuff behind pointers, use runtime checks for safety, or change how you represent data to something suboptimal.
Re:“I just started writing some Rust” by wtetzner on Monday January 28, 2019 @09:54AM (#19017817)
Sure, but the inverse is true too. Sometimes you can get better performance in Rust, because the borrow checker allows you to do things you would never do in C/C++. For example, passing around array slices is very common in Rust, but the cases where you'd do it in C or C++ is much more limited, because it's so error prone. You have no guarantees that the memory being pointed to won't be pulled out from under your feet.
Re:“I just started writing some Rust” by SamReidHughes on Monday January 28, 2019 @03:40PM (#19020862)
I've had Rust hinder my ability to pass around array slices more than help.
Re:“I just started writing some Rust” by wtetzner on Tuesday January 29, 2019 @04:41PM (#19030053)
The point is that while it's easy to do in C++, it's extremely error prone.

Rust encourages you to write code that works with things like slices and references instead of copying, because the compiler won't let you use them in a way that is error prone.

Re:“I just started writing some Rust” by SamReidHughes on Tuesday January 29, 2019 @08:40PM (#19031795)
That's some nice sounding evangelism, but also completely besides the point. You seem to think I don't understand C++ and Rust. Believe me, I do. Rust inhibits use cases of array slicing that are both useful and not error prone.

Last time I read a blog post about this, they picked a situation that was perfectly safe and ordinary in C++, with straightforward function-local safety reasoning.

Re:“I just started writing some Rust” by wtetzner on Thursday January 31, 2019 @03:56PM (#19048769)
Just out of curiosity, do you have an example where Rust got in your way when trying to use an array slice?
Re:“I just started writing some Rust” by SamReidHughes on Sunday February 03, 2019 @06:36AM (#19069057)
Approximately, here is one example. I wanted to use something like a Vec<&[u8]> as a means of passing a set of buffers to a function. Likewise it made sense to return such a value from another function, g. The way g worked was to read or hold a large buffer into memory, e.g. 1 megabyte in size, and then parse out the slices from it. So maybe you'd want to return something that looks like the C++ type

    struct Foo {
    private:
        vector<uint8_t> buf;
    public:
        // points into buf
        vector<pair<const uint8_t *, size_t>> slices;
    };
Well you can't do that. Obviously there are workarounds, like to return an object of type Foo holding the buf, with an api like impl Foo { fn getSlices(&self) -> Vec<&[u8]> }. Internally the object holds a Vec<(usize, usize)> or something like that, and you have a bunch of translation logic around your API's. And extra work to allocate the return value. So that's one example of Rust getting in your way.

Probably some others would be uses of Interval<Buf>, and some cases where functions return Buf in https://github.com/srh/nihdb . I wanted to use &[u8] to represent the bounds of intervals, and IIRC that generally involved passing intervals upwards into functions, but for some reason I can't remember it got annoying and I couldn't be bothered to do it.

Re:“I just started writing some Rust” by steveklabnik on Sunday February 03, 2019 @07:35AM (#19069271)
This is a pain point, but it’s not due to slices; it’s the “self-referential struct problem.” There are various solutions, as you note, but it can be annoying, it’s true. It’s a tough one; in the general case, it’s saving you from problems, but when you know you’re not going to hit those edge cases, it’s less than ideal.
Re:“I just started writing some Rust” by wtetzner on Tuesday February 05, 2019 @02:46PM (#19089018)
Is there a theoretical reason for the "self-referential struct problem", or is it just an artifact of the current borrow-checker implementation?

It doesn't seem unsafe to have a struct field refer to another member of that struct, but maybe I'm missing something.

Re:“I just started writing some Rust” by steveklabnik on Wednesday February 06, 2019 @05:25AM (#19094049)
It's not theoretical, it's practical. Here's some code with a self-referential struct:

https://play.rust-lang.org/?version=stable&mode=debug&editio...

Here, we have a self-referential struct. If you run this, you may get different numbers than me, but

  [src/main.rs:17] &f = Foo {
      x: 5,
      p: 0x00007ffcbbba41c0
  }
Here, p points to x. It's all good. The address of f is

  [src/main.rs:19] &f as *const Foo = 0x00007ffcbbba41b8
We move f into oh_no. Its address changes:

  [src/main.rs:25] &f as *const Foo = 0x00007ffcbbba3f28
... but p does not:

  [src/main.rs:26] &f = Foo {
      x: 5,
      p: 0x00007ffcbbba41c0
  }
Any access of p is now a use-after-free.

Does that make sense?

Re:“I just started writing some Rust” by wtetzner on Wednesday February 06, 2019 @08:03AM (#19094751)
OK, so the issue is that references aren't updated when a move occurs. That does make sense.

So to make this work, references would need to be re-written when a move/copy occurs.

I can still see having an easy way to construct self-referential structs being a useful thing, even if the compiler prevents you from moving them. Maybe with a smart clone() method that can update references correctly.

However, I am a little confused about this specific example. I don't understand why oh_no() taking ownership causes f to be copied to a new location. Shouldn't it remain in the same place on the stack? I feel like I'm missing something.

Re:“I just started writing some Rust” by steveklabnik on Wednesday February 06, 2019 @08:26AM (#19094863)
> So to make this work, references would need to be re-written when a move/copy occurs.

Yes! C++ has a concept called "move constructors" that allows for this (this would be that "smart clone" you talk about later in the comment), but we made a decision to not include it. This introduces some nice properties, at the cost of disallowing self-referencing structs.

> I can still see having an easy way to construct self-referential structs being a useful thing, even if the compiler prevents you from moving them.

So, in some sense, this is what the new Pin stuff is about. It lets you say "from this point on, this thing isn't going to move again" and therefore be self-referential.

> I don't understand why oh_no() taking ownership causes f to be copied to a new location.

"Taking owernship" means "move". "move" means "a memcpy from the old to the new location." Rust isn't really special from any other sort of language with value semantics here, other than disallowing you to use the old value, since it was moved out from. Does that make sense?

Re:“I just started writing some Rust” by wtetzner on Wednesday February 06, 2019 @10:28AM (#19095807)
So, in regards to why it's generally not safe to move a self-referential struct, I understand now. My confusion about the memcpy is a tangent ;)

I guess my confusion is that I while realized "move" could mean a memcpy, I didn't realize it always meant a memcpy.

In other words, I thought the compiler would be smart enough to realized the memcpy is unnecessary, even though ownership has been transferred as far as the type system/borrow checker is concerned.

As another example, if you have this situation:

    let x = 45;
    dbg!(x);
    let y = x;
    dbg!(y);
Is there really any reason to allocate separate memory for y, if x is in accessible after y is defined?

Or is this just an implementation detail, and could possibly change in the future?

Also, thanks for being so responsive! I suppose this isn't really the best place to have this discussion ;)

[EDIT] I realize that's not a good example, because 45 will just be copied. But you get what I was trying to show.

Re:“I just started writing some Rust” by steveklabnik on Wednesday February 06, 2019 @03:03PM (#19099255)
Semantically, it’s always a memcpy. In practice, the copy can be elided depending on circumstance. But that’s an optimization, not the semantic.

(And yeah, Copy types are like that too; the only semantic difference between Copy and move is if you can use the old binding.)

Any time! And yeah, the rust forums are probably better but it’s no big deal :)

Re:“I just started writing some Rust” by wtetzner on Wednesday February 06, 2019 @04:36PM (#19100268)
OK, cool, so in this case it's just that the Rust compiler isn't optimizing away the memcpy. Thanks!
Re:“I just started writing some Rust” by SamReidHughes on Tuesday February 05, 2019 @04:12AM (#19083899)
I didn't say it was specific to slices. The inconvenience around array slices is because they have a pointer. The non-pointery aspects of Rust slices are an advantage over C++. (E.g. not having pointer arithmetic.)
Re:“I just started writing some Rust” by steveklabnik on Tuesday February 05, 2019 @04:24AM (#19083927)
The thread started out by saying slicing, specifically.

Regardless, no worries, I was just trying to add clarity around details, just in case.