These are chat archives for rust-lang/rust

18th
Dec 2016
Alexander Ronald Altman
@pthariensflame
Dec 18 2016 00:02
Can you not put a cfg on blocks?
(I'm not sure.)
Matanel Levi
@matanelevi
Dec 18 2016 00:11

@pthariensflame like this?

#[cfg(feature = "x")]
{
    use std::io::{Read, Write};
    use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
}

The use statement affects only the current scope, isn't it?

Alexander Ronald Altman
@pthariensflame
Dec 18 2016 00:13
Yeah, so I guess even if that works it doesn't help your use case.
The next thing I'd try is make all of them pub uses and put them in a module that you immediately import * from, then put the config on the wrapper module and its import.
But that feels really hacky. :s
If only Rust had auto-imported anonymous modules like Agda! XD
Matanel Levi
@matanelevi
Dec 18 2016 01:05
Some knows an IDE / editor with macro run-time debugging feature?
@pthariensflame Oh shit, I just put #[cfg(feature = "x")] before each one :D
Dave Hylands
@dhylands
Dec 18 2016 01:22
Matanel Levi
@matanelevi
Dec 18 2016 01:23
@dhylands cool! :)
Thank you
xialvjun
@xialvjun
Dec 18 2016 12:02
struct Foo<'a> {
    x: &'a i32,
}

struct<'a, 'b> Foo<'a> where 'a:'b {
    x: &'b i32,
}
Of course I know there isn't the second syntax, but I think it's much more clearly. And using the first and right syntax, I don't know what it mean word by word, and I don't know when I came across a new state, how should I use the lifetimes...
Matanel Levi
@matanelevi
Dec 18 2016 12:36
Run-time macro debugging will ever be supported?
How do you deal with it?
Aleksey Kladov
@matklad
Dec 18 2016 12:38
@matanelevi not sure what is run-time macro debuging is (Rust macros exist at compile time only), but this may be helpful: https://danielkeep.github.io/tlborm/book/mbe-min-debugging.html
Matanel Levi
@matanelevi
Dec 18 2016 12:42

@matklad thanks, but I meant is this:
Let's look at this code:

define_exercise_impl! {
    pub fn exercise_0(arg: String) {
        format!("Hello {}", arg)
    }
}

define_exercise_impl generates a wrapper on exercise_0.
The cost of this action is that I can't debug exercise_0 at run-time

@matklad I can't step-into the function and debug each line
Aleksey Kladov
@matklad
Dec 18 2016 12:45
Ah, you want to debug macro generated code! I am not familiar with tools to do this. But, debugging generated code is painful in any language. Do you really need a macro there though :) ?
Matanel Levi
@matanelevi
Dec 18 2016 12:49
@matklad what the wrapper does, it to generate a function that evaluates the block of the original function, encodes it and sends the result through a socket. In other words, the original function can return ANY value.
How don't know how to do this without a macro
Aleksey Kladov
@matklad
Dec 18 2016 12:50
Can you show how the macro is defined?
Matanel Levi
@matanelevi
Dec 18 2016 12:50
macro_rules! generate_exercise_wrapper {
    ($func_name:ident, 
     $inner_blk:block,
     $blk_input_var:ident,
     $input_type:ty) => (
         pub fn $func_name<RW>(stream: &mut RW) -> ExerciseResult
            where RW: Read + Write
        {
            // Read input
            let input_len = stream.read_u32::<BigEndian>().expect("Read input len");
            let mut input_encoded = vec![0u8; input_len as usize];
            stream.read_exact(input_encoded.as_mut_slice()).expect("Read input");

            // Decode input
            let $blk_input_var: $input_type = 
                rustc_serialize::decode(input_encoded.as_slice()).expect("Decode input");

            // Compute answer
            let answer = $inner_blk;

            // Encode answer
            let answer_encoded = 
                rustc_serialize::encode(&answer, SizeLimit::Infinite).expect("Enocde answer");

            // Send answer
            stream.write_u32::<BigEndian>(answer_encoded.len() as u32).expect("Write answer len");
            stream.write_all(answer_encoded.as_slice()).expect("Write answer");

            // Read result
            let result_len = stream.read_u32::<BigEndian>().expect("Read result len");
            let mut result_encoded = vec![0; result_len as usize];
            stream.read_exact(result_encoded.as_mut_slice()).expect("Read result");

            // Decode result
            let ex_result: ExerciseResult = 
                rustc_serialize::decode(result_encoded.as_slice()).expect("Decode result");

            ex_result
        }
     )
}
@matklad ^
Aleksey Kladov
@matklad
Dec 18 2016 12:52
Hm, I think you can use a high order function for this
Matanel Levi
@matanelevi
Dec 18 2016 12:52
@matklad You got me, what have you thought about?
Aleksey Kladov
@matklad
Dec 18 2016 12:53
fn wrapper<R: RustcEncodable>(inner_fn: &Fn(String) -> R)
Matanel Levi
@matanelevi
Dec 18 2016 12:54
@matklad Ah, notice that Fn should be able to accept any argument type
@matklad But it is limited to a single argument
Aleksey Kladov
@matklad
Dec 18 2016 12:54
fn wrapper<In: RustcDecodable, Out: RustcEncodable>(inner_fn: &Fn(In) -> Out)
Matanel Levi
@matanelevi
Dec 18 2016 12:56
@matklad Well, you're good. What unique about Rust is that you must adjust your view on designing
Aleksey Kladov
@matklad
Dec 18 2016 13:19
For posterity, here is a bit of somewhat working code: http://play.integer32.com/?gist=a752745dd468bea33d79d346eb9eb97c&version=stable
Matanel Levi
@matanelevi
Dec 18 2016 14:59

I can't compile this:

type ExerciseFunc = Fn(&mut TcpStream) -> ExerciseResult;

let mut stream = TcpStream::connect("127.0.0.1:6567").unwrap();

let exercises: Vec<Box<ExerciseFunc>> =
        vec![Box::new(|stream| exercises::wrapper(stream, exercises::exercise_0))];

where wrapper is:

pub fn wrapper<RW, InnerFn, In, Out>(stream: &mut RW, inner_fn: InnerFn) -> ExerciseResult
    where RW: Read + Write,
          In: Decodable,
          Out: Encodable,
          InnerFn: Fn(In) -> Out {}

and where exercise_0 is:

pub fn exercise_0(arg: String) -> String {
    format!("Hello {}", arg)
}

Cargo's output:

rror[E0277]: the trait bound `Box<for<'r> std::ops::Fn(&'r mut std::net::TcpStream) -> exercises::exercise_result::ExerciseResult>: std::ops::Fn<(_,)>` is not satisfied
  --> src\main.rs:31:15
   |
31 |         match exercises::wrapper(&mut stream, ex_func) {
   |               ^^^^^^^^^^^^^^^^^^ trait `Box<for<'r> std::ops::Fn(&'r mut std::net::TcpStream) -> exercises::exercise_result::ExerciseResult>: std::ops::Fn<(_,)>` not satisfied
   |
   = note: required because of the requirements on the impl of `std::ops::Fn<(_,)>` for `&Box<for<'r> std::ops::Fn(&'r mut std::net::TcpStream) -> exercises::exercise_result::ExerciseResult>`
   = note: required by `exercises::exercises_impls::wrapper`

error[E0277]: the trait bound `Box<for<'r> std::ops::Fn(&'r mut std::net::TcpStream) -> exercises::exercise_result::ExerciseResult>: std::ops::FnOnce<(_,)>` is not satisfied
  --> src\main.rs:31:15
   |
31 |         match exercises::wrapper(&mut stream, ex_func) {
   |               ^^^^^^^^^^^^^^^^^^ trait `Box<for<'r> std::ops::Fn(&'r mut std::net::TcpStream) -> exercises::exercise_result::ExerciseResult>: std::ops::FnOnce<(_,)>` not satisfied
   |
   = help: the following implementations were found:
   = help:   <Box<std::boxed::FnBox<A, Output=R> + 'a> as std::ops::FnOnce<A>>
   = help:   <Box<std::boxed::FnBox<A, Output=R> + Send + 'a> as std::ops::FnOnce<A>>
   = note: required by `exercises::exercises_impls::wrapper`
Someone can help? :worried:
Matanel Levi
@matanelevi
Dec 18 2016 15:53

Oh, the problem was in this code:

for (i, ex_func) in exercises.iter().enumerate() {
        print!("Running exercise_{:.<25}", i);

        match exercises::wrapper(&mut stream, ex_func) { }

The match statement wan't updated

Emanuel Tapia
@learneman
Dec 18 2016 18:01
Has anyone installed built redox? i'm getting this error. Any idea how to fix?
   Compiling orbtk v0.2.5
error[E0277]: the trait bound `&'a mut orbclient::Window: orbclient::Renderer` is not satisfied
  --> /home/emanuel/.cargo/registry/src/github.com-1ecc6299db9ec823/orbtk-0.2.5/src/window.rs:45:52
   |
45 |             font.render(&c.to_string(), 16.0).draw(&mut self.inner, x, y, color)
   |                                                    ^^^^^^^^^^^^^^^ the trait `orbclient::Renderer` is not implemented for `&'a mut orbclient::Window`
   |
   = help: the following implementations were found:
   = help:   <orbclient::Window as orbclient::Renderer>
   = note: required for the cast to the object type `orbclient::Renderer`

error: aborting due to previous error

   Compiling html5ever v0.10.3
error: Could not compile `orbtk`.

To learn more, run the command again with --verbose.
Makefile:443: recipe for target 'filesystem/ui/bin/browser' failed
make: *** [filesystem/ui/bin/browser] Error 101