These are chat archives for rust-lang/rust

10th
Aug 2018
qq00
@qq00
Aug 10 2018 00:58

I have a function which returns a Result<T, E>

how do I write a line of the form
if (..) {
return a E;
}

qq00
@qq00
Aug 10 2018 01:10
What is the correct behaviour for a Drop that runs into an error ?
Azriel Hoh
@azriel91
Aug 10 2018 02:38
@qq00
first one, probably return Err(<your error type>);
second, if it can handle it then it should, i guess it's hard to give a "good" answer
qq00
@qq00
Aug 10 2018 03:55
I see "self.into()" alot -- what is this idiom, and what does it do ?
David Vo
@auscompgeek
Aug 10 2018 03:58
qq00
@qq00
Aug 10 2018 03:59
Makes sense now; thanks!
Akos Vandra
@axos88
Aug 10 2018 06:06
Hey guys. Is it possible to create a lowercase fn() dynammically from code? Problem is that we need to send it to a C libarary for callback and the library does not accept, nor return any userdata, so we can’t distinguish WHY that cb is executed. Unless we can somehow generate an fn() for each registered callback, and save the userdata within them (as literals perhaps?), we won’t be able to dynamically register and unregister them… I know, this would be pretty hacky in any language, but can it be done in rust? I guess it to work something like this (not real code below):
my_callback_withuserdata(u32) {
  process_callback_and_notify_based_on_userdata
}

extern “C”  fn my_bare_callback() { 
    my_callback_withuserdata(495) 
}

generate_callback(user_data: u32) -> extern “C” fn() {
  my_bare_callback.clone().replace_495_with(user_data).compile()
}
Denis Lisov
@tanriol
Aug 10 2018 08:14
Technically this can be done on most systems, but requires a nontrivial amount of platform-specific code.
I think this should be possible to do in a more or less portable way using jit, gccjit or something like that.
Sylwester Rąpała
@xoac
Aug 10 2018 09:01
On the survey of rust 2018 was question about what is lacking in rust. I found https://github.com/RReverser/serde-xml-rs need a new maintainer(RReverser/serde-xml-rs#65). There are also changes in master that are not published to crates io.
btw. has anyone idea how to hack #[serde(deny_unknown_fields)] if it's not supported by crate?
Akos Vandra
@axos88
Aug 10 2018 09:49
@tanriol thanks. We discussed just creating a pool of X functions with a macro, and using a global variable to map the userdata and the actual function they will execute. Not exactly the pretties solution, but we’ll explore it before turning to JIT
Sylwester Rąpała
@xoac
Aug 10 2018 12:24

Oo and I have a second suggestion for rust 2018 survey. There could be a way to say that some crate has a critical patch. For example if we use in Cargo.toml:

hyper-tls = "0.11.4"

And there is a security issue in this crate. Author of that crate should publish information and next time you run cargo build -- u get information that you use unsecured dependencies (as warnning). And when the version is "0.11" cargo should avoid that version. What u think?

Denis Lisov
@tanriol
Aug 10 2018 12:39
Do you mean integrating something like cargo-audit?
A Dinesh
@dineshadepu
Aug 10 2018 13:17
I am using macro write!
Generally to use a macro we need to do #[macro_use] extern crate std;
but in this case, with out macro_use statement we can use the write! macro
But we should import the Write trait, which I understand corectly
Because of macro explansin
Does the prelude provide us the write! macro
Sylwester Rąpała
@xoac
Aug 10 2018 13:21
@tanriol exactly :)
Do Duy
@juchiast
Aug 10 2018 14:13

Hi, can we use RUST_BACKTRACE in wasm32-unknown-emscripten yet? I tried but got this error:

exception thrown: abort("Unwind_GetIPInfo") at Error
.... very long error log

I'm compiling with cargo-web, so it can be either an error of cargo-web or rustc.

Mahmut Bulut
@vertexclique
Aug 10 2018 15:15
Hi guys, are there any way to serialize str which has a varying size into [u8; 64] without std ?
Dealing with this for a long time.
Zakarum
@omni-viral
Aug 10 2018 15:16
@vertexclique How do you mark the end of str? Even if it fit into [u8; 64]
Mahmut Bulut
@vertexclique
Aug 10 2018 15:17
null terminator imo, union dropping might work right?
After the Pr that is merged 11 days ago, it doesn't anymore. Complains about boundary checking beyond the union boundaries.
Zakarum
@omni-viral
Aug 10 2018 15:20
fn string_to_array(stirng: &str) -> [u8; 64] {
    let mut result: [u8; 64] = [0; 64];
    let bytes = string.as_bytes();
    assert!(bytes.len() < 64);
    result[..bytes.len()].copy_from_slice(bytes);
    result
}
@vertexclique Something like this?
Mahmut Bulut
@vertexclique
Aug 10 2018 15:25
hmm does core::str::Bytes have as_bytes ?
Zakarum
@omni-viral
Aug 10 2018 15:26
str does
Mahmut Bulut
@vertexclique
Aug 10 2018 15:30
oh forgot to say that, this is called from macro.
so needs to be in const fn
p.s. i tried with static mut [0x00_u8; 64] and assigning to beginning of it inside the fn.
No chance.
Mahmut Bulut
@vertexclique
Aug 10 2018 15:36
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
Zakarum
@omni-viral
Aug 10 2018 16:11
@vertexclique if it is a constant - can't you just init bytes array with literal?
tandrysyawaludin
@tandrysyawaludin
Aug 10 2018 16:14
Anoyone know how to solve this?

I use Rocket Framework for POST API

#[post("/user", format = "application/json", data = "<user>")]
fn new_user(user: Json<User>) -> T { ... }

but when I try to request with xhr via browser with code like this

var data = JSON.stringify({
      "email": "2aa@.a",
      "password": "pass"
    });

    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;

    xhr.addEventListener("readystatechange", function () {
      if (this.readyState === 4) {
        console.log(this.responseText);
      }
    });

    xhr.open("POST", "http://localhost:8000/user");
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.setRequestHeader("Cache-Control", "no-cache");

    xhr.send(data);

I always get 404 not found

Mahmut Bulut
@vertexclique
Aug 10 2018 16:32
@omni-viral Can you give me an example ?
Zakarum
@omni-viral
Aug 10 2018 16:39
static const BYTES: &'static [u8] = b"TheLiteral";
Mahmut Bulut
@vertexclique
Aug 10 2018 16:45
static VERSION: [u8; 64] = *b"1.0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
already did that, not beautiful. that was my last resort.
Judson Lester
@nyarly
Aug 10 2018 17:49
Is there a way (or will there soon be a way) to install Clippy on a non-nightly system?
Zakarum
@omni-viral
Aug 10 2018 18:11
@vertexclique But you need not zeros
tandrysyawaludin
@tandrysyawaludin
Aug 10 2018 19:09
.
tandrysyawaludin
@tandrysyawaludin
Aug 10 2018 20:08
anyone know how to implement CORS in rust?
Denis Lisov
@tanriol
Aug 10 2018 20:17
@tandrysyawaludin Look at the framework you're using
Mahmut Bulut
@vertexclique
Aug 10 2018 20:17
@omni-viral jep don't need that but need to fill that 64 - str length bytes
Zakarum
@omni-viral
Aug 10 2018 20:18
@vertexclique So can you just write those bytes down in code in the form of literal?
Judson Lester
@nyarly
Aug 10 2018 21:43
I found this: rust-lang-nursery/rust-clippy#2527 Is the comment that Clippy will always be nightly accurate?