These are chat archives for rust-lang/rust

13th
Jun 2017
Da Xian Sheng
@daxiansheng
Jun 13 2017 06:32
@uwearzt blog.japaric.io is a greate blog. I like it.😊😊
Da Xian Sheng
@daxiansheng
Jun 13 2017 06:42
:wink:
Erich Cordoba
@ericho
Jun 13 2017 23:03

From this discussion regarding the performance of running yes | pv > /dev/null I copied the following code:

use std::io::{stdout, Write};

fn main() {
    let buf = "y\n".repeat(4096).into_bytes();
    let out = stdout();
    let mut myout = out.lock();
    loop {
        myout.write(&buf).unwrap();
    }
}

It turns out, that if I compile with cargo build --release I get [7.25GiB/s] which is the same as the GNU's yes command. But in debug mode I get [7.49GiB/s].

Actually in both cases the values goes from 6.8 to 7.5 GiB/s but in debug mode the values seems to be greater that release mode.
Any idea on why this is happening?

mhsjlw
@mhsjlw
Jun 13 2017 23:38
error[E0507]: cannot move out of borrowed content
  --> src/protocol/unconnected_pong.rs:23:29
   |
23 |         buffer.write_string(self.server_name).unwrap();
   |                             ^^^^ cannot move out of borrowed content

error: aborting due to previous error(s)

unconnected_pong:

// with fn x(&self)
buffer.write_string(self.server_name).unwrap();

write_string trait:

fn write_string(&mut self, n: String) -> Result<()> {
        let length: u16 = n.len() as u16;
        self.write_u16::<BigEndian>(length).unwrap();
        self.write(&n.as_bytes()).unwrap();
        Ok(())
    }
how can I fix this?
Denis Lisov
@tanriol
Jun 13 2017 23:39
What does self refer to?
mhsjlw
@mhsjlw
Jun 13 2017 23:40
in which context?
for unconnected_pong:
struct UnconnectedPong {
    pub ping_id: i64,
    pub server_id: i64,
    pub server_name: String
}
for write_string:
pub trait WriteBytesCustom: io::Write {
Denis Lisov
@tanriol
Jun 13 2017 23:41
Actually, does not matter. Your write_string is suboptimal... passing an owned String is usually not what you need.
mhsjlw
@mhsjlw
Jun 13 2017 23:41
what should I be using instead?
@tanriol str is fixed, so that's not it. String is like the Vec of str, it is varying in size, that's what I want. Should it be a borrowed string?
like pub server_name: &String
will that fix it
Denis Lisov
@tanriol
Jun 13 2017 23:46
I'd try defining write_string as fn write_string<T: AsRef<str>>(&mut self, n: T) -> io::Result<()>;
Or possibly just fn write_string(&mut self, n: &str) -> io::Result<()>;
You don't need to modify the string in this method, so you don't need to passed a String here.
And you can always borrow a String as an str with &*my_string
mhsjlw
@mhsjlw
Jun 13 2017 23:49
What should I do when I read it then
this: fn read_string(&mut self) -> Result<&str> ?
i currently have fn read_string(&mut self) -> Result<String>
Denis Lisov
@tanriol
Jun 13 2017 23:51
You don't need to modify that one. Yes, the API is not exactly symmetrical, and that's normal.
mhsjlw
@mhsjlw
Jun 13 2017 23:51
thank you for your help
so pub server_name: str with buffer.write_string(&self.server_name).unwrap(); is good?
Denis Lisov
@tanriol
Jun 13 2017 23:53
You can't store an unboxed str, it is unsized and has to be behind a reference. I'd go for pub server_name: String and buffer.write_string(&*self.server_name).
mhsjlw
@mhsjlw
Jun 13 2017 23:54
Ok, I have one more question if you don't mind
I'm not sure how I should handle reading from my UDP socket, because the packets I'm reading aren't fixed sizes
and it doesn't implement io::Read
so I have to recv_from
and I can't pass my Socket down to each packet for decoding
that question didn't make sense
let me think