These are chat archives for rust-lang/rust

5th
Dec 2018
Florian Beeres
@cideM
Dec 05 2018 00:14

I'm trying to use Rust for advent of code instead of Haskell and of course lifetimes (I think?) are proving to be an issue. Each day is modeled as a struct

#[derive(Clone)]
pub struct DayProg<'a, T: Display> {
    pub name: &'static str,
    pub run: fn(&'a str) -> Result<&'a T, Error>,
}

The days are stored in a hashmap and then retrieved via a key passed in through the CLI.

    let mut day_progs = HashMap::new();
    day_progs.insert("day1", day1_prog);

    if let Some(d) = matches.value_of("day") {
        let prog = day_progs.get(d).expect("No program for that day! :(");
        print!("{}", (prog.run)(&input)?);
    };

I then want to run a particular program by calling its run function with the input data (from stdin or file). Now here's the issue... I want to express that the &str in the struct (or rather its run fn) lives as long as the user input data. But I get

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> src/main.rs:54:21
   |
54 |     let day1_prog = DayProg {
   |                     ^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `str`

I'm sure this is simple to a seasoned rustacean but even with SO/reddit/docs I can't figure it out or rather I don't understand why my solution does not work. Any help, including pointing to some enlightening source so I can figure it out myself, would be greatly appreciated :)

David Lewis
@davidarmstronglewis
Dec 05 2018 00:19
Does anyone know how to pass a mpsc::Sender into a Rayon into_par_iter? I’m getting an error that Sender doesn’t implement Sync, which makes sense - however calling .clone() on my sender doesn’t seem to fix the issue.
I’m really struggling with this.
David Lewis
@davidarmstronglewis
Dec 05 2018 00:24
I mean, I’d like to communicate the progress of my rayon iterators via a cloned sender channel to the main thread.
Florian Beeres
@cideM
Dec 05 2018 00:26
Re: my issue adding pub struct DayProg<'a, T: Display + ?Sized> makes the compiler happy.
David Lewis
@davidarmstronglewis
Dec 05 2018 01:49
I got it working. Nvm! :P
Paul Hauner
@paulhauner
Dec 05 2018 04:03
Thank you @lchoran and @tanriol!
Zhang Cheng
@Matrix-Zhang
Dec 05 2018 07:32
hi, how to use macro as global with edition 2018? for example, derive serde_derive's macro for anywhere in project
Tim Robinson
@1tgr
Dec 05 2018 07:35
@cideM this seems an odd function signature:
pub run: fn(&'a str) -> Result<&'a T, Error>,
The only &'a T you can make from an &'a str is another &'a str (or a &'static str)
Which presumably limits you to retrurning substrings of the original input, or string literals
I would suggest run returns an owned T instead of a reference
T can be &'a str to retain the current behaviour, or it can be anything else
Florian Beeres
@cideM
Dec 05 2018 07:56
@1tgr Yeah that's a very valid and good point and makes total sense
Ali Shirvani
@alishir
Dec 05 2018 10:23
Hi there, sorry for cross posting. I implemented simple udp server with std::UdpSocket, I can receive packets from nc but I cound't receive RTP packets from baresip. I got ICMP error port unreachable when using baresip as a sender.
I read about the cause of error and the following paragraph seems relative:
This is indicative of an overload condition or process priority configuration problem in the reporting host. The process in question was swapped out of memory and was not able to swap back in quickly enough to avoid the unreachable indication.
rsb007
@rsb007
Dec 05 2018 10:27
in this code there is no varaible name bytes but author apply extend_from_slice_error on it ansd also please tell about use of # in this code
quote! {
let field_value = self.#field.into_cdrs_value();
bytes.extend_from_slice(field_value.into_cbytes().as_slice());
};
Denis Lisov
@tanriol
Dec 05 2018 10:52
@rsb007 Looks like a proc macro fragment. I'd suggest starting with quote!
trsh
@trsh
Dec 05 2018 11:19
It is a proc macro, but I dont understand the question
let field = Ident::new("kaka", Span::call_site());
quote! {
let field_value = self.#field.into_cdrs_value(); // this will be compiled as `let field_value = self.kaka.into_cdrs_value();`
bytes.extend_from_slice(field_value.into_cbytes().as_slice());
};
trsh
@trsh
Dec 05 2018 11:24
Not sure about Ident::new, maybe can be just str
David O'Connor
@David-OConnor
Dec 05 2018 19:05

Hey dudes. Basic question about packages. I made a lib that's designed as an API. Works with example files in the lib's src directory. I'm trying to test it as a user would, with the exception of reffing the lib locally instead of on crates.io. In my test crate, I have this in Cargo.toml:

edition = "2018"

[dependencies]
mylib = {path = "../mylib"}

When I try to use it, with use mylib in the example's src/lib.rs, I receive the following error:

error[E0432]: unresolved import `mylib`
 --> src\lib.rs:7:5
  |
7 | use rebar;
  |     ^^^^^ no `mylib` external crate

Is there something I must add to my lib's lib.rs ? I'm sure the path is correct, for I get a Cargo.TOML error if I replace it with a bogus path.

David O'Connor
@David-OConnor
Dec 05 2018 19:48
Edit: Sorted. This line in my cargo.toml was breaking things:
[lib]
crate-type = ["cdylib"]