These are chat archives for rust-lang/rust

24th
May 2018
Berkus Decker
@berkus
May 24 2018 00:33
@tandrysyawaludin you're welcome!
tandrysyawaludin
@tandrysyawaludin
May 24 2018 02:13
error: unused dynamic parameter: `id`
  --> src/supplier_module/mod.rs:24:9
   |
24 | #[get("/<id>")]
why? even though i've used it
tandrysyawaludin
@tandrysyawaludin
May 24 2018 02:24
ooh sorry that is my false, i forgot to use id in the function.
Farzeen
@thefzsalam
May 24 2018 04:54

Why is move out of dereference of raw pointer an error?

error[E0507]: cannot move out of dereference of raw pointer

The explanation at https://doc.rust-lang.org/error-index.html#E0507 doesn't say anything about raw pointers.
I was trying to claim ownership of an instance of a #[repr(C)] struct MyObjectfor returning from MyObject::new().
A pointer to MyObject is returned by gobject_ffi::g_object_new().
I need to make sure that the g_object_ref() is called to increase reference count when Cloning and g_object_unref() is called when Dropping.

Dmitriy
@dpogretskiy
May 24 2018 06:17
@thefzsalam you can't take ownership of the value itself, https://doc.rust-lang.org/std/ptr/struct.Unique.html
this can be helpful though.
Zakarum
@omni-viral
May 24 2018 10:02
@thefzsalam you can do it with unsafe function std::ptr::read
But you should be sure that you not doing anything wrong

The problem with just

move out of dereference

is that when you dereference a pointer the value is borrowed

while std::ptr::read is the way to tell compiler "I really want to move the value from the pointer, I promise that the pointer will not be dereferenced (because it's UB) unless std::ptr::write is used to write new value there"
But from your explanation I see that this is not your case
Denis Lisov
@tanriol
May 24 2018 10:09
@thefzsalam What are you trying to do? Export a GObject API for a Rust object?
Farzeen
@thefzsalam
May 24 2018 11:23
@omni-viral, g_object_new() writes a value there. So I don't have to do std::ptr::write.
@tanriol, I don't want to export. I was building an implementation of GListModel for passing into gtk_list_bind_model
Zakarum
@omni-viral
May 24 2018 12:11
@thefzsalam std::ptr::read is solution to get ownership of the object from raw pointer.
It is not valid for your use-case
What you can do is to make your own smart-pointer to call g_object_ref() and g_object_unref().
Like
struct GPtr<T>(*mut T);

impl<T> Deref for GPtr<T> {
  type Target = T;
  fn deref(&self) -> &T {
    unsafe { &*self.0 }
  }
}

impl<T> Clone for GPtr<T> {
  fn clone(&self) -> GPtr<T> {
    g_object_ref(self.0)
    GPtr(self.0)
  }
}

impl<T> Drop for GPtr<T> {
  fn drop(&mut self) {
    g_object_unref(self.0);
  }
}
And MyObject::new will return GPtr<MyObject>
Probably someone already made such smart-pointer. You can look for it on https://crates.io
Zakarum
@omni-viral
May 24 2018 12:18
Note that you cannot obtain &mut T from GPtr as it is semantically equivalent to Rc
So you may want to have another pointer type that 'owns' and object. That type will not be clonnable but convertible to clonnable GPtr
Farzeen
@thefzsalam
May 24 2018 12:46
That's what I was looking for.
What you can do is to make your own smart-pointer to call g_object_ref() and g_object_unref().
Thanks @omni-viral :)
Daniel Garrido
@danie16arrido
May 24 2018 16:06

hi all I am trying to run a simple example for the reqwest crate

fn main() {
    let text = reqwest::get("https://www.rust-lang.org")?
    .text()?;

    println!("body = {:?}", body);
}

..and I get this error

error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)

my setup:
cargo 0.26.0 (41480f5cc 2018-02-26)
rustc 1.25.0 (84203cac6 2018-03-25)
reqwest 0.8.5
any ideas?

Andrey Lesnikov
@ozkriff
May 24 2018 16:08
update to rust 1.26 and try
fn main() -> Result<(), ()> {
    let text = reqwest::get("https://www.rust-lang.org")?
        .text()?;
    println!("body = {:?}", body);
    Ok(())
}
or stay on rust 1.25 and wrap your code in a helper function returning result
Daniel Garrido
@danie16arrido
May 24 2018 16:16
Thanks @ozkriff I will update to 1.26 stable :)