These are chat archives for rust-lang/rust

6th
Jun 2018
Farzeen
@thefzsalam
Jun 06 2018 03:38
@trsh, IsInEffectAsOfTimestamp variable name and seconds type? We usually use snake_case for variables and CamelCase for type name.
@trsh, does seconds and libc::time_t have same layout? If not, it will cause segfaults.
trsh
@trsh
Jun 06 2018 05:46
@thefzsalam was my mistake. I was not passing a reference
Can somebody suggest how to code this with raw rust pointers?
STUDENT* students = malloc(numStudents * sizeof *students);
    for (x = 0; x < numStudents; x++){
        students[x].firstName=(char*)malloc(sizeof(char*));
        students[x].lastName=(char*)malloc(sizeof(char*));
    }
??
Farzeen
@thefzsalam
Jun 06 2018 05:52
@trsh, use libc crate to call malloc and allocate numStudents number
*number of STUDENT instances
If the members firstname and lastNameare of type *mut libc::char then you can do libc::malloc(..) them too.
I'm not sure whether rust allows treating pointers as arrays. So proably doing ptr[..] wont work, where ptr is a pointer. There are functions like ptr::offse
Farzeen
@thefzsalam
Jun 06 2018 05:58
std::ptr::offset(..) in std library that allows you to do that.
trsh
@trsh
Jun 06 2018 06:01
@thefzsalam but offset as I know is read feature, not write
Thats the only part I do not understand
Im trying this now
let addrsss_ptr: *mut ns1__AddressToValidate = libc::malloc(
        std::mem::size_of::<ns1__AddressToValidate>() * 2) as *mut ns1__AddressToValidate;


    addrsss_ptr.offset(0).write(adrs);
trsh
@trsh
Jun 06 2018 06:15
Looks it's working
Roman M
@sintanial
Jun 06 2018 08:10

Hi all, anybody help me please :).
I try to build static library for iOS. When i run cargo lipo --release i catch error

How to fix this ?

error[E0425]: cannot find function `_Unwind_Backtrace` in module `uw`
  --> /Users/roman/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.2.3/src/backtrace/libunwind.rs:54:13
   |
54 |         uw::_Unwind_Backtrace(trace_fn, &mut cb as *mut _ as *mut _);
   |             ^^^^^^^^^^^^^^^^^ not found in `uw`

error: aborting due to previous error

This error received because in backtrace.rs has this line

        // No native _Unwind_Backtrace on iOS
        #[cfg(not(all(target_os = "ios", target_arch = "arm")))]
        pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
                                 trace_argument: *mut c_void)
                    -> _Unwind_Reason_Code;

so, what i need to do to resolve this problem ?

GrumpyMetalGuy
@GrumpyMetalGuy
Jun 06 2018 11:08
Hi all. I have an enum with several members in it, some of which have associated data (e.g. MyFoo(i32), MyBar). I need to pass a vector of acceptable enum types to another function, so I'm unable to use pattern matching. Is there any way to specify MyFoo without the i32 data element, or do I need to move to one enum to describe the type, and another to hold an optional value? (sorry, on phone right now, so can't copy code, sorry for formatting!)
Michal 'vorner' Vaner
@vorner
Jun 06 2018 11:24
The latter. But I've seen at least one crate that helps you generate the other one without code duplication.
Zakarum
@omni-viral
Jun 06 2018 11:31
Can this trigger UB?
use std::{mem, slice};

#[repr(C)]
struct Foo {
  x: u8,
  y: u32,
}

fn main() {
    let foo = &[Foo { x: 1, y: 2 }];
    let foo: &[u8] = unsafe { slice::from_raw_parts(mem::transmute(foo.as_ptr()), mem::size_of::<Foo>()) };
    let bar = [0, 1, 2, 3, 4];
    println!("{}", bar[foo[0] as usize]); // Should be fine.
    println!("{}", bar[foo[1] as usize]); // Can lead to UB?
}
Michal 'vorner' Vaner
@vorner
Jun 06 2018 11:35
I think it can. Because there can/will be padding in between x and y and it may be uninitialized memory. And touching uninitialized memory in Rust is UB in most of the times.
trsh
@trsh
Jun 06 2018 12:31
Hey is there a way to compile rust code from String?
Zakarum
@omni-viral
Jun 06 2018 12:32
There definitely will be padding
The question is: "Does rust define value for padding bytes?"
trsh
@trsh
Jun 06 2018 12:33
Like rustc "fn main()\n {}\n"
?
Zakarum
@omni-viral
Jun 06 2018 12:34
@trsh Write it to file and run rustc
trsh
@trsh
Jun 06 2018 12:36
@omni-viral I know I can do that :)... that wasn't the question.
Zakarum
@omni-viral
Jun 06 2018 12:36
I guess this is the only option
trsh
@trsh
Jun 06 2018 12:37
Seems so.
Zakarum
@omni-viral
Jun 06 2018 12:37
You can make rustcstr shell command or executable that will write string to file and run rustc for you :smile:
trsh
@trsh
Jun 06 2018 12:38
Hehe, sure I can :D
Thank you for your guidance Yoda
(bowing)
:D
Denis Lisov
@tanriol
Jun 06 2018 12:50
@omni-viral Do you mean cargo-script? ;-)
trsh
@trsh
Jun 06 2018 12:52
hehe
@tanriol thanks, just what I needed
trsh
@trsh
Jun 06 2018 12:57
Documentation is abit out of date. Commands do not work as suggested
Andrey Lesnikov
@ozkriff
Jun 06 2018 12:59
try installing a fresh version from git
cargo install cargo-script --git https://github.com/DanielKeep/cargo-script.git
trsh
@trsh
Jun 06 2018 13:01
Ok, will try.. Tnx for suggestion @ozkriff
Sebastian Blei
@iamsebastian
Jun 06 2018 13:02
Are we able to install a specific commit from GitHub via cargo config?
I know about branches, but not about unmerged pull requests ... A dependencies dependency is too old and I can't compile diesel.rs API, due to the old chrono version of tinyhttp, which is part of multipart
Sebastian Blei
@iamsebastian
Jun 06 2018 13:04
Thanks @ozkriff. Got the doc open, but did not see the rev key.
trsh
@trsh
Jun 06 2018 13:04
Yeah, works. I wonder where it dumps the depencies after runing
Andrey Lesnikov
@ozkriff
Jun 06 2018 13:05
~/.cargo/script-cache/
trsh
@trsh
Jun 06 2018 13:05
And it's cleaned at some point?
Ok whatever, that's not an major issue for me
Andrey Lesnikov
@ozkriff
Jun 06 2018 13:06
hmm, you are right, only project files are there. compiled dependencies are somewhere else (still in some subdirectory of ~/.cargo i think)
trsh
@trsh
Jun 06 2018 13:33
let path = env::current_dir().unwrap();
  let pp = format!("{}{}", path.display(), "/rtests/kk.rs");
  let comm = format!("rustc +nightly --target wasm32-unknown-unknown -O {}", pp);

  println!("{}", &comm);

  let mut child = Command::new(&comm)
    .spawn()
    .expect("what?");
Rust keeps stupidly telling "No such file or directory"
Command works as charm when running from terminal
I guess it's this thing > rust-lang/rust#21571 ?
Michal 'vorner' Vaner
@vorner
Jun 06 2018 13:39
You're trying to run the whole long thing as a binary. I expect binary "rustc +nightly --…" really doesn't exist and that you want to run "rustc" with the rest as arguments.
trsh
@trsh
Jun 06 2018 13:39
@vorner how then I can run same command in terminal? From any location?
Michal 'vorner' Vaner
@vorner
Jun 06 2018 13:40
Because shell takes that thing apart for you. It splits by whitespace.
bash (or other thing) in your terminal is a language and it "translates" from the whole long string.
trsh
@trsh
Jun 06 2018 13:42
@vorner what do u suggest?
use .args right?
Yeah! Tnx man
Michal 'vorner' Vaner
@vorner
Jun 06 2018 13:46
Yes. Or, if it comes to worst, run something like /bin/sh and pass it -c and your command as parameters. It'll then do the same as the shell in the terminal. But that might be dangerous if there's an input from user, because then the user can embed interesting things like $(rm -rf /) and stuff.
trsh
@trsh
Jun 06 2018 13:47
My example works with args perfectly
trsh
@trsh
Jun 06 2018 13:53
Damn wasm is huge
Even with fn main() {} only
I gueess im doing smth wrong :)
Do Duy
@juchiast
Jun 06 2018 14:20
My wasm app weights about 300kb when optimized. How much is yours? @trsh
trsh
@trsh
Jun 06 2018 14:22
@juchiast around the same. But where is the catch? One from the demo is so small > https://www.hellorust.com/demos/add/add.wasm
Do Duy
@juchiast
Jun 06 2018 14:24
Wow, that is much smaller
trsh
@trsh
Jun 06 2018 14:27
108kb
x2
For you x3
And when I oped it's like 100x smaller - i dont undesrtand
Do Duy
@juchiast
Jun 06 2018 14:28
it's 108b :D
trsh
@trsh
Jun 06 2018 14:29
Fck
yes
So?
@juchiast any ideas, why our's ar so big :D ?
Do Duy
@juchiast
Jun 06 2018 14:30
my wasm contains yew and webstd dependencies, so I'm not sure how small it can possibly be
trsh
@trsh
Jun 06 2018 14:31
Mine is on from tut.
#[no_mangle]
pub extern "C" fn add_one(x: i32) -> i32 {
    x + 1
}
trsh
@trsh
Jun 06 2018 14:37

![no_std]

Andy Bell
@Andy-Bell
Jun 06 2018 14:37
that would be why
trsh
@trsh
Jun 06 2018 14:37
This helps
Now it's bytes
Andy Bell
@Andy-Bell
Jun 06 2018 14:38
yeah, I imagine even compiling well the rust std lib would end up adding up
sitty
@sitty
Jun 06 2018 14:40
Is there a Date or DateTime datatype in rust std lib?
Michal 'vorner' Vaner
@vorner
Jun 06 2018 14:42
@sitty There's something in std::time. Depends on what you need. These types can represent the time, but don't know much about nice formatting. You'd need something like the chrono crate for that.
trsh
@trsh
Jun 06 2018 14:49
chrono is ok
sitty
@sitty
Jun 06 2018 14:54
thanks @trsh , @vorner I'm looking at chrono now
Dylan DPC
@Dylan-DPC
Jun 06 2018 14:59
chrono is looking for co-maintainers. So if you want to join in, this is a good time :P
Michal 'vorner' Vaner
@vorner
Jun 06 2018 15:06
@Dylan-DPC Any info what work would that be? I'm not opposed to helping out (though I haven't yet make any changes to that specific crate), but I don't have much free time.
Dylan DPC
@Dylan-DPC
Jun 06 2018 15:07
for chrono?
Michal 'vorner' Vaner
@vorner
Jun 06 2018 15:07
Yes
Dylan DPC
@Dylan-DPC
Jun 06 2018 15:07
we haven't created the org yet so i'm not sure.
the first thing would be is to address existing issues & PRs
sitty
@sitty
Jun 06 2018 15:42
is there an easier way to add a dependency in cargo.toml like say npm add instead to hand coding it?
Do Duy
@juchiast
Jun 06 2018 16:09
This could be a nice feature to add to cargo
Ash
@ashthespy
Jun 06 2018 17:42
Hi - I am trying to use the timer crate but it seems to want time::duration::Duration vs my std::time::Duration
So - what is the best way to go about this?
Michal 'vorner' Vaner
@vorner
Jun 06 2018 19:06
Isn't the timer crate like super-ancient?
Ash
@ashthespy
Jun 06 2018 19:51
then what should I be using?
Fredrik Portström
@portstrom
Jun 06 2018 20:22
@ashthespy Depends on what kind of program you are making. If you're doing network communication in your program, you're probably using Tokio and you should use its timer feature. If you're using a GUI toolkit in your program, it probably has a timer feature and you should use that.
Aleksandr Lykhouzov
@lykhouzov
Jun 06 2018 22:09

Hi, i am learning rust and obviously i have an issue E0507
there is an example of code

pub fn run(&self) -> LaunchError {
        // Launch an application
        let rkt = &self.rocket;
        for p in &self.plugins {
            for (base, rs) in &p.get_routes() {
                rkt.mount(base, rs.to_vec());
            }
        }
        rkt.attach(Template::fairing()).launch()
    }

and i cannot understand how to fix it

error[E0507]: cannot move out of borrowed content
  --> src/app/mod.rs:57:17
   |
57 |                 rkt.mount(base, rs.to_vec());
   |                 ^^^ cannot move out of borrowed content

error[E0507]: cannot move out of borrowed content
  --> src/app/mod.rs:60:9
   |
60 |         rkt.attach(Template::fairing()).launch()
   |         ^^^ cannot move out of borrowed content

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0507`.
Azriel Hoh
@azriel91
Jun 06 2018 22:24
do #mount and #attach take self?
Aleksandr Lykhouzov
@lykhouzov
Jun 06 2018 22:25
let me check
it looks like yes
pub fn mount(mut self, base: &str, routes: Vec<Route>) -> Self {

ok. thanks for good question:))
I've changed to

rkt = rkt.mount(base, rs.to_vec());

and it compiles now

Azriel Hoh
@azriel91
Jun 06 2018 22:32
(disclaimer, I'm not a veteran at Rust)
two thingies:
  1. the earlier line let rkt = &self.rocket; says "let me borrow rkt", but then the later line rkt.mount(..) says "give rkt away to mount". But you're not allowed to give away what you're only borrowing (hence E507)
  2. the function signature feels weird, because fn mount(mut self, ..) says "I want to claim ownership of self, but I'm also signalling I'm going to change it"
    the weird part is, the function should just state that it's claiming ownership, and since callers will no longer have ownership, the function shouldn't also have to say "by the way I'm doing things to the thing I now own"
if the function is meant to just mutate the rocket and return it every time (so callers don't have to reassign), then it can just take a mutable borrow (&mut self)
the earlier line then would change to let rkt = &mut self.rocket;, because you'd then want to say "I want to borrow rkt and change it"
Aleksandr Lykhouzov
@lykhouzov
Jun 06 2018 22:42
Thank you for the explanation. I am a bit confused with this borrowing topic. so thanks for this good explanation.
mount function goes from Rocket crate, not sure that i can modify it at my level of knowledge:)