These are chat archives for rust-lang/rust

5th
Jul 2018
Kelly Thomas Kline
@kellytk
Jul 05 2018 05:03
After reading https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#build-dependencies I'm still uncertain as to the purpose of build-dependencies and how it differs from dependencies if someone would enlighten me?
Andrey Lesnikov
@ozkriff
Jul 05 2018 05:20
they are used by build.rs and are not linked in the final artifact
Kelly Thomas Kline
@kellytk
Jul 05 2018 05:39
Thanks
James Sewell
@jamessewell
Jul 05 2018 06:02
Is there a way I can truncate (not quite the right word) a file at a byte position?
So say I want to write x bytes to a file at position 1024 then remove all content after position 2048
Or I suppose another way of looking at it resize a file to x bytes
that’ll truncate/grow the file to the size you specify
James Sewell
@jamessewell
Jul 05 2018 06:50
Is there a way of finding out the current seek position?
Or do I need to track that myself?
Christian Witts
@ChristianWitts
Jul 05 2018 07:04
I don’t see an implementation of tell sadly
although I see this commit from 2011, from the pre-1.0 days
rust-lang/rust@71de17d
Turing Zhu
@Turing-Chu
Jul 05 2018 07:14
error: The binary show_posts is specified by packages diesel_demo_step_3_mysql v0.1.0 (file:////XXXX/diesel/examples/mysql/getting_started_step_3) and diesel_demo_step_1_pg v0.1.0 (file:///XXXXXX/diesel/examples/postgres/getting_started_step_1) but can be documented only once. Consider renaming or marking one of the targets as doc = false.
does someone know how to set doc=false?
James Sewell
@jamessewell
Jul 05 2018 07:30
Bugger
Turing Zhu
@Turing-Chu
Jul 05 2018 07:31
it’s not a bug. just using wrong way. thks.
Sylwester Rąpała
@xoac
Jul 05 2018 08:07
has anyone know a crate that have a enum that hold any number and implements From with support serde (that resolve to just proper number)? I mean sth like this:
pub enum AnyNum {
U8(u8),
...
U64(u64),
F32(f32),
F64(f64)
}
Sylwester Rąpała
@xoac
Jul 05 2018 08:39
btw it's so easy
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Value {
    U32(u32),
    I32(i32),
    F32(f32),
    F64(f64),
}
Ingvar Stepanyan
@RReverser
Jul 05 2018 08:42
@jamessewell @ChristianWitts You can use Cursor for that https://doc.rust-lang.org/std/io/struct.Cursor.html
Sylwester Rąpała
@xoac
Jul 05 2018 09:17
how can I create external serializer function that take extra parameter? I have sth like default in my struct and wanna skip if default is exactly the same.
Ingvar Stepanyan
@RReverser
Jul 05 2018 09:22
@xoac you can just use custom serialize_with for that field
Christian Witts
@ChristianWitts
Jul 05 2018 09:25
oh nice, thanks @RReverser. I missed that as I was searching for std::io::fs tell and ftell. Did not think to look for position. :thumbsup:
Sylwester Rąpała
@xoac
Jul 05 2018 10:46

@RReverser from https://serde.rs/field-attrs.html

function must be callable as fn<S>(&T, S) -> Result<S::Ok, S::Error> where S: Serializer

but I need (don't work :( complain about result Type):
pub fn ser_opc_data<S>(data: &OpcData, dq: OpcQuality, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer + SerializeSeq,

I want to serializa OpcData depends on dq
Sylwester Rąpała
@xoac
Jul 05 2018 11:39
is this possible?
for now I don't know how to get S::Ok and S::Error
Ingvar Stepanyan
@RReverser
Jul 05 2018 11:42
the result should just come from serializer.serialize_* you're calling
As for parameter - I don't think it's possible, you need to either implement some trait (Default?) using which you can retrieve the default value in that function or implement separate function just for that field
Sylwester Rąpała
@xoac
Jul 05 2018 11:45
But this default value is not know at compile time
I have struct that holds a lot of OpcData and then I need to find the most common
and then serialize
Ingvar Stepanyan
@RReverser
Jul 05 2018 11:46
Huh
Sylwester Rąpała
@xoac
Jul 05 2018 11:46
OpcData don't need to be marked Serialize I just need a function that I can call to serialize that object. Or past that code to serialize of HolderOpcData (is what I don't wanna do)
Ingvar Stepanyan
@RReverser
Jul 05 2018 11:46
Then you need to implement custom Serialize on the structure itself
I'm not sure there is an easy way for what you want
Sylwester Rąpała
@xoac
Jul 05 2018 16:05
How to manually do what #[serde(rename = "m")] do?
Zakarum
@omni-viral
Jul 05 2018 16:42
@xoac By manually you mean when implementing Serialize by hand?
Sylwester Rąpała
@xoac
Jul 05 2018 16:42

To be more clear:

#[derive(Debug)]
pub struct Payload {
    #[serde(with = "serde_millis")]
    ts: SystemTime,
    #[serde(rename = "h")]
    header: Option<Header>,
    #[serde(rename = "m")]
    meta: Option<MetaData>,
    // I wanna to serialize this field in special way
    // depending on values in header
    // Data don't implement Serialize on it's own
    data: Data,
}

So I would like to serialize 3 first fields exactly with their Serialize implementation. I can't find examples on more complicated custom serialization..

@omni-viral yes exactly
You can just pass not the actual name of the field
Sylwester Rąpała
@xoac
Jul 05 2018 16:44
ok. So the one is done. What withwith = "serde_millis"?
Sylwester Rąpała
@xoac
Jul 05 2018 16:45
I saw that. I just don't know how to call that by hand
Zakarum
@omni-viral
Jul 05 2018 16:46
There is serialize function
Call it
Sylwester Rąpała
@xoac
Jul 05 2018 16:47
impl Serialize for Payload {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serde_millis::serialize::serialize(&self.ts,serializer)?;
    }
like this?
Zakarum
@omni-viral
Jul 05 2018 16:48
No.
You need to pass something into serialize_field that wraps SystemTime and implements Serialize using serialize
Like
struct WithMillis<'a, T>(&'a T);

impl<'a, T> Serialzie for WithMillis<'a, T>
where
  T: Milliseconds,
{
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serde_millis::serialize::serialize(&self.0, serializer)?;
    }
}
And then
impl Serialize for Payload {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        struct_serializer.serlialize_field("ts", WithMillis(&self.ts))
    }
Sylwester Rąpała
@xoac
Jul 05 2018 16:55
but for WithMillis I can use serde_derive and parameter with?
isan_rivkin
@Isan-Rivkin
Jul 05 2018 17:05
I have an issue with Web3-Rust implementation https://github.com/tomusdrw/rust-web3 I am trying to execute this code https://play.rust-lang.org/?gist=c12aabfc4656662437e8fdc8d2c3707d&version=stable&mode=debug&edition=2015 and i get an error on line #19, the problem is with byte_report. if anyone could help this would be wonderful because im quite lost with this lib. This is the Tokenize trait the error speaks about https://docs.rs/web3/0.3.0/web3/contract/tokens/trait.Tokenize.html and this is the function Im calling https://docs.rs/web3/0.3.0/web3/contract/struct.Contract.html#method.call
The error im getting the trait web3::contract::tokens::Tokenize is not implemented for (web3::types::H160, web3::types::Bytes)
Kelly Thomas Kline
@kellytk
Jul 05 2018 17:44
How could I take a &'static str from .render() on line 12 of https://pastebin.com/CNkc2Nwy so that it matches the signature on line 1?
Denis Lisov
@tanriol
Jul 05 2018 17:51
If you need to render it just once in the execution, you can leak it.
Kelly Thomas Kline
@kellytk
Jul 05 2018 17:57
Rendering it once is my goal, so that actix web can serve it many times directly from memory. I'll read about leak thanks
A Dinesh
@dineshadepu
Jul 05 2018 19:20
Hello guys. I am trying to make my n body code parallel
I see many examples on concurrency only having a single loop
I haven't seen parallelizing two for loops
Can some one point me to some resource where I can parallelize the above code
Kelly Thomas Kline
@kellytk
Jul 05 2018 21:55
Regarding https://vpaste.net/OcM8A the move || suggests that contentstr and system ownership will copy into the closure of the new HttpServer instance, correct?
Colt Frederickson
@coltfred
Jul 05 2018 22:35

Noob question about rust typclass resolution. I'm trying to use alga to call .inverse() on a value. I get

error[E0283]: type annotations required: cannot resolve `T: alga::general::Inverse<_>`
   --> src/internal/test.rs:176:25
    |
176 |         let z_inv:T = z.inverse();
    |                         ^^^^^^^

I have a trait in scope that contains an Inverse<Multiplicative>, but I don't see a way to specify that at the call site.

Denis Lisov
@tanriol
Jul 05 2018 22:42
@coltfred Could you show the whole function?
Colt Frederickson
@coltfred
Jul 05 2018 22:48
@tanriol Minimal example is:

fn test<T: Field>(x:T) -> T { x.inverse()}
Field extends something which has extends AbstractQuasigroup<Multiplicative> which Extends Inverse<Multiplicative>
It's something about the way it's trying to resolve .inverse. Is there a long hand way to do this in Rust?
tsoernes
@tsoernes
Jul 05 2018 22:54

These two snippets do not behave the same for me:

/// Alt 1:
let env = Env { .. };
let mut state = MyState { 0: env.state.clone() } 
let mut next_state;
for i in 0..K {
    fn1(&state);
    env.step(); // Modifies 'env.state'
    next_state = MyState { 0: env.state.clone() };
    fn2(&state, &next_state);
    state = next_state;
}

/// Alt 2:
let env = Env { .. };
let mut state = MyState { 0: env.state.clone() } 
for i in 0..K {
    fn1(&state);
    env.step(); // Modifies 'env.state'
    let next_state = MyState { 0: env.state.clone() };
    fn2(&state, &next_state);
    let state = next_state;
}

Why?

Denis Lisov
@tanriol
Jul 05 2018 22:54
<x as Inverse<Multiplicative>>::inverse()
Colt Frederickson
@coltfred
Jul 05 2018 22:56
error[E0061]: this function takes 1 parameter but 0 parameters were supplied
  --> src/internal/test.rs:51:31
   |
51 | fn test<T: Field>(x:T) -> T { <x as Inverse<Multiplicative>>::inverse()}
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 parameter
Denis Lisov
@tanriol
Jul 05 2018 22:57
@coltfred Oops, looks like you have to pass x explicitly in this case...
@tsoernes The let state inside the loop does not affect the outer variable and is dropped at the end of the iteration.
tsoernes
@tsoernes
Jul 05 2018 23:01
@tanriol then what if you have a function returning 2 variables as a tuple -- how do you change them from iteration to iteration? let (mut a, mut b); then (a, b) = fn(...) inside the loop gives syntax errors
Denis Lisov
@tanriol
Jul 05 2018 23:06
Many options. For example, let mut my_tuple = (i1, i2), or let (na, nb) = fn(...) ; a = na; b = nb;, or maybe something else entirely.