These are chat archives for rust-lang/rust

21st
Jun 2017
Serhii Plyhun
@snuk182
Jun 21 2017 08:13
did any1 work with cocoa-rs?
I'm looking for a way to attach single-/double-click evt listeners to the NSControl
https://stackoverflow.com/a/6954203/817019 this is how it works with ObjC, but Rust bindings obviously does not have this
Michal 'vorner' Vaner
@vorner
Jun 21 2017 16:29

I have an enum, like this:

  enum Content {
      Interesting {
         event_type: EventType,
         server: Option<Server>,
         remote: String,
      },
      Other {},
  }

Now, under certain conditions, I want to modify a field inside the Interesting variant. I have this code, but it seems somewhat too hairy. Is there a better way? Aside from being very verbose and having to list all the fields, it doesn't describe my intention (modify one field in the variant) and instead looks like something completely different (replace the whole thing from grounds up).

             if let Content::Interesting { server: None, event_type: EventType::SshHoneypot, remote } = event.tags {
                 event.tags = Content::Interesting { server: Some(Server::SshHoneypot), event_type: EventType::SshHoneypot, remote };
             }
Nikolay Denev
@ndenev
Jun 21 2017 17:22

Hi, I'm trying to understand why one of the values here is dropped while the other not, for example these look equivalent but are not:

    let r = CString::new(i).unwrap().as_bytes_with_nul().as_ptr();

the above one does not work, while this one does:

    let c = CString::new(i).unwrap();
    let r = c.as_bytes_with_nul().as_ptr();

both compile.

In one case r points to the string in the other it's empty.
Denis Lisov
@tanriol
Jun 21 2017 18:54
@ndenev In the second case your CString c lives till the end of the block; in the first your CString is an (unnamed) temporary value which is dropped at the end of the statement.
Nikolay Denev
@ndenev
Jun 21 2017 19:01
This is what I figured too, but it really feels unintuitive.
On the other hand when messing with unsafe and raw pointers I should probably make sure I know what I'm doing :D
Denis Lisov
@tanriol
Jun 21 2017 19:03
I'm not sure about the upcoming non-lexical lifetimes... won't be surprised if it is dropped in both cases.
Nikolay Denev
@ndenev
Jun 21 2017 19:06
So then I have to use Box::into_raw() or something?
Denis Lisov
@tanriol
Jun 21 2017 19:10
Actually, no, test2 should be fine IIUC... unless you call a C function that stores the string somewhere.
Nikolay Denev
@ndenev
Jun 21 2017 19:11
I'm calling a C function in the real code that uses this, but the C code is making a copy, so then it should be fine.
Syed Faraaz Ahmad
@faraazahmad
Jun 21 2017 19:28
Hello Folks!
I'm just getting started with Rust
Anton Ugryumov
@dvec
Jun 21 2017 19:30
Hello
Syed Faraaz Ahmad
@faraazahmad
Jun 21 2017 19:33
where can i find some good tutorials
other than rustbyexample
or the docs
Syed Faraaz Ahmad
@faraazahmad
Jun 21 2017 19:41
thanks!
Nikolay Denev
@ndenev
Jun 21 2017 19:52
@faraazahmad Both rustbyexample and "the book" are very good. Also you can also check out sites like codewars.com and exercism.io . Both have Rust tracks and they are fun.
Syed Faraaz Ahmad
@faraazahmad
Jun 21 2017 20:00
thanks @ndenev i needed sometihng like "learning by making projects". exercism seems like the way to go
stevensonmt
@stevensonmt
Jun 21 2017 21:09
@tanriol do you mind walking me through the lifetimes in those functions?
Nikolay Denev
@ndenev
Jun 21 2017 21:28
I think the issue is that the raw pointer lifetime is separate from the lifetime of the pointed to value. So in the first case we get the raw pointer to a value created on the same line which goes out of scope at the end of that same line.
Denis Lisov
@tanriol
Jun 21 2017 21:32
The raw pointer does not carry any lifetime information. If it were a borrow from the CString, you would have received an error about a reference outliving the object referred to.
Nikolay Denev
@ndenev
Jun 21 2017 21:38
Makes sense.
stevensonmt
@stevensonmt
Jun 21 2017 22:34
Thanks @ndenev , that makes sense.