These are chat archives for rust-lang/rust

12th
Sep 2018
Sam Johnson
@sam0x17
Sep 12 2018 01:11
I don't understand why I'm getting this error as everything is set up to take an FnMut closure -- where is this requirement to be Fn and not FnMut coming from?
    pub fn on_data(&mut self, callback: impl FnMut(&[u8]) + 'a) {
        self.data_callback = Some(Box::new(callback));
    }

    pub fn connect(&mut self, target: &'static Stream<'static>) {
        self.on_data(&|data: &[u8]| { target.write(data); });
    }
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
  --> src/stream.rs:66:23
   |
66 |         self.on_data(&|data: &[u8]| { target.write(data); });
   |              -------  ^^^^^^^^^^^^^   ------ closure is `FnMut` because it mutates the variable `target` here
   |              |        |
   |              |        this closure implements `FnMut`, not `Fn`
   |              the requirement to implement `Fn` derives from here

error: aborting due to previous error
Sam Johnson
@sam0x17
Sep 12 2018 01:49
ok I actually got it to compile with the following, but is there any way I can get rid of that move?
    pub fn connect(&mut self, target: &'a mut Stream<'a>) {
        self.on_data(move |data: &[u8]| { target.write(data) });
    }
mtak-
@mtak-
Sep 12 2018 03:19
@sam0x17 in the final code, you're only moving references so it is pretty optimal.
i.e. copying two pointers
Sam Johnson
@sam0x17
Sep 12 2018 03:44
@mtak- thanks that makes me feel better, I just wish I didn't have to specify move when I use on_data as this makes the API for on_data ugly but I don't see a way around it unless there is some way I can encode the move into the signature of on_data itself
Zakarum
@omni-viral
Sep 12 2018 06:41
@sam0x17 you were trying to use reference to closure as argument
Note that FnMut is implemented for mutable reference to FnMut.
Immutable reference to Fn also implements FnMut. Therefore compiler tried to implement Fn for your closure and failed.
So you just need to remove move
trsh
@trsh
Sep 12 2018 13:08
DO not understand this
#[allow(unused_imports)]
#[derive(json_fedex_bind)]
#[schema = "AddressValidationRequest"]
struct AddressValidationIn;
warning: unused import: `json_fedex_bind`
  --> manu/src/applications/api/externapis/fedex/addressvalidation.rs:37:10
   |
37 | #[derive(json_fedex_bind)]
   |          ^^^^^^^^^^^^^^^
   |
   = note: #[warn(unused_imports)] on by default
1) It's not unused, the derive ads code. 2) I Have that allow
And compiler doesn't care
Farzeen
@happycoder97
Sep 12 2018 13:34
    struct Foo(Option<String>);

    let a = Foo(Some("Hello".to_owned()));

    if let Some(val) = &a.0 {
        println!("val = {}", val);
    }
How does this work?
I thought this won't compile because it is matching Option with &Option
Lyle Mantooth
@IslandUsurper
Sep 12 2018 13:57
@trsh, maybe that should be #![allow(unused_imports)]. Because I don't expect a struct to have any imports in it. Though I also don't know how a derive can trigger that warning either.
@happycoder97, a recent Rust version made that more ergonomic. I forget which one. After 1.20, though.
trsh
@trsh
Sep 12 2018 13:58
@IslandUsurper ! is for whole file, isn't it? Then I won't get other warinings
Lyle Mantooth
@IslandUsurper
Sep 12 2018 13:59
It's for the containing scope, which most people use for the whole file. But I think that attribute is being applied to the struct (as the following scope) instead of the derive.
trsh
@trsh
Sep 12 2018 14:00
Ehh, I had some actually unused imports in my qoute!ed code
And it's reported this way
nm
Lyle Mantooth
@IslandUsurper
Sep 12 2018 14:03
@happycoder97, it was 1.26: https://blog.rust-lang.org/2018/05/10/Rust-1.26.html Look for "Nicer match bindings"
Farzeen
@happycoder97
Sep 12 2018 14:48
Thanks @IslandUsurper :+1:
Farzeen
@happycoder97
Sep 12 2018 15:04
let foo = Some("foo".to_owned());
    match foo {
        Some(ref bar) => {
            println!("{}", bar);
        }
        _ => {}
    }
I understand how the ref keyword works in this example, but I can't put it into words like I can do with & (take reference of a variable) and * (dereference a reference) operators. Could anyone help?
Lyle Mantooth
@IslandUsurper
Sep 12 2018 15:11
Best thing I ever read about it, which made it click for me, was something like "ref creates a reference in a pattern when there isn't one on the right-hand side."
May be better to say "where there isn't one"
Farzeen
@happycoder97
Sep 12 2018 15:16
:+1:
Sylwester Rąpała
@xoac
Sep 12 2018 15:52

I want to use unstable feature:

  1. I run cargo +nightly build --examples
  2. at begin of my example I use ![feature(int_to_from_bytes)]
  3. I got error:
error[E0599]: no method named `to_bytes` found for type `u64` in the current scope
  --> examples/print_evs_dump.rs:27:38
   |
27 |     let bytes: [u8; 8] = test_number.to_bytes();
   |                                      ^^^^^^^^

error: aborting due to previous error

unstable method: https://doc.rust-lang.org/std/primitive.u64.html#method.to_bytes

Denis Lisov
@tanriol
Sep 12 2018 16:19
The methods have been renamed to always mention the desired endianness (documentation)
Kelly Thomas Kline
@kellytk
Sep 12 2018 20:56
+1
Ichoran
@Ichoran
Sep 12 2018 22:51
from_str has a type argument. It doesn't just produce serde_json::Value by default. If you want that, you have to ask: serde_json::from_str::<Value>(...)
Very often it can figure out what you want via type inference, but match could match anything, so that doesn't narrow it down at all.
Kelly Thomas Kline
@kellytk
Sep 12 2018 22:54
Thank you @Ichoran
Ichoran
@Ichoran
Sep 12 2018 22:55
(Maybe you figured that out yourself already since you removed the question!)
Kelly Thomas Kline
@kellytk
Sep 12 2018 23:04
Yes :-)