These are chat archives for rust-lang/rust

24th
Aug 2018
Kelly Thomas Kline
@kellytk
Aug 24 2018 00:07
I would like to write Rust on a box with no internet access. I'm running into "failed to load source for a dependency on x" errors due to crates.io not being available. Is there a solution short of cloning the entire crates index and mirroring it on my offline box?
Azriel Hoh
@azriel91
Aug 24 2018 00:51
Kelly Thomas Kline
@kellytk
Aug 24 2018 01:41
"Updating registry" stalls, can it be downloaded and installed another way?
Zakarum
@omni-viral
Aug 24 2018 06:48
@axyz It would be better if I could see context
Michal 'vorner' Vaner
@vorner
Aug 24 2018 07:01
@cradee_gitlab What is the problem? You can create multiple futures/tasks and spawn multiple of them and tokio takes care of the rest
I'm trying out rust 2018 with a fairly large project. after resolving all the changes needed, i'm stuck on this error:
error[E0428]: the name `_` is defined multiple times note: `_` must be defined only once in the macro namespace of this module
there's no information to tell me where it's being defined, and anyway it seems that if it's really something called _, that shouldn't be a problem since _ is a placeholder
i don't have any top-level things called _ in any case
Andrea Moretti
@axyz
Aug 24 2018 08:17

@omni-viral I have a branch with the issues and everything is open.
https://github.com/optimal-image/optimal-image/pull/16/files#diff-7fa25e825b0c0b1e45b9dbb6fc1af69fR138 this is the memory free that if uncommented causes segfault

https://github.com/optimal-image/optimal-image/pull/16/files#diff-7fa25e825b0c0b1e45b9dbb6fc1af69fR104 this is the function that should convert from ImageData to VipsImage, but the generated VipsImage is then segfaulting if you try to convert it back to ImageData using the previous function.

Carl Dong
@dongcarl
Aug 24 2018 09:02
Hi all, how would I get a &T from a &Box<T>
&*
superriva
@superriva
Aug 24 2018 09:33
Deref makes auto convert &Box<T> to &T:
fn main() {
    let foo = 2;
    let bar = Box::new(foo);
//    let baz = &bar; // baz is &Box<i32>
//    f(&*baz);
    f(&bar);
}

fn f(a: &i32) {
    println!("{}", a);
}
suyuanhan
@suyuanhan
Aug 24 2018 09:34
What's different between & and ref ?
Vitaly
@Virtuos86
Aug 24 2018 09:35
In match expression?
Kelly Thomas Kline
@kellytk
Aug 24 2018 10:41
Why does the dependency yew = { git = 'https://github.com/DenisKolodin/yew' } cause cargo vendor to generate a sync error "the source path is not an existing regular file"? I need to access the dependency either using git or path because I am developing off of a release that hasn't been sync'd to crates yet
Michal 'vorner' Vaner
@vorner
Aug 24 2018 11:02
I guess because nobody wrote the support to vendor from a git repo. But you could probably put it into a git submodule and refer to it not as git =, but path =.
Kelly Thomas Kline
@kellytk
Aug 24 2018 11:03
@vorner I cloned the repo, changed the git = to path =, and the error remains
Michal 'vorner' Vaner
@vorner
Aug 24 2018 11:05
:-(. Then I suggest searching cargo-vendor's issue tracker. If there's one about that, there might be a workaround written. If not, you can open it and they could add support for at least ignoring path = dependencies quickly. Maybe.
I think vendoring and offline compilation is not yet very well trodden path yet.
Kelly Thomas Kline
@kellytk
Aug 24 2018 11:06
@vorner Pardon me I was mistaken. With path = the command finishes but yew is ignored from the "Vendoring" list and it's not in the vendor directory
Michal 'vorner' Vaner
@vorner
Aug 24 2018 11:07
That'd be fine, because you then you have the sources in path, not in the vendor directory ‒ but you still have it around.
Kelly Thomas Kline
@kellytk
Aug 24 2018 11:08
alexcrichton/cargo-vendor#25 path = isn't supported
True
Fantasized
@Fantasized
Aug 24 2018 11:15
newbie question, let some:i8 = 0 Is the only way to zero initialize a variable?
was kinda expecting stuff like let some:i8( ) or let some:i8{ }
Dmitriy
@dpogretskiy
Aug 24 2018 11:17
let some = 0i8;
superriva
@superriva
Aug 24 2018 11:18
you could use Default:
fn main() {
    let a: i8 = Default::default();
    println!("{}", a);
}
Kelly Thomas Kline
@kellytk
Aug 24 2018 11:22
Thanks @vorner vendor + path = solved the issue
Fantasized
@Fantasized
Aug 24 2018 11:22
yeah, thought about that, but think of this scenario
fn somefn () - > i8{ } here, I don't wanna type 0i8 or call Default::default(); I simply wanna return like fn somefn () - > i8{ () }
seems like I couldn't do that.
Dmitriy
@dpogretskiy
Aug 24 2018 11:34
you're trying to return () as i8 type
and those are different types
Fantasized
@Fantasized
Aug 24 2018 11:39
I didn't know () has a type, all I thought was it would call the constructor like function for any type T ,since primitive types doesn't have constructor I thought it would be zero initialized.
Dmitriy
@dpogretskiy
Aug 24 2018 11:43
if you want to make it generic over primitive types, then there probably are ways
() has type () which is empty tuple called unit btw, same as haskell does it
Vitaly
@Virtuos86
Aug 24 2018 12:01
@Fantasized Why do you need this? Any use case?
Zakarum
@omni-viral
Aug 24 2018 12:09
@axyz g_object_unref is wrong function to free memory I suppose. It takes refcounted object like VipsImage not just allocated memory
From what I've found it seems you need to use free from libc
Zakarum
@omni-viral
Aug 24 2018 12:43
I wonder if it is possible to automatically collect all safety notes for all unsafe operations in unsafe block and insert them as comment to the begining of the block.
Michal 'vorner' Vaner
@vorner
Aug 24 2018 12:54
There's some kind of convention how to write safety notes, except for like free-form comments?
Zakarum
@omni-viral
Aug 24 2018 13:01
There is convention to write them under /// # Safety
Michal 'vorner' Vaner
@vorner
Aug 24 2018 13:16
Ah, I see. Not like explanation inside the unsafe block why it does nothing wrong, but documentation on the unsafe functions.
Zakarum
@omni-viral
Aug 24 2018 13:18
Providing safety notes from unsafe functions to the unsafe block where user can explain for each one why usage is valid.
Like this
unsafe {
    <safety note from `foo`>: <explanation why it is valid>
    <safety note from `bar`>: <explanation why it is valid>
    <safety note from `baz`>: <explanation why it is valid>
    foo(bar(baz()));
}
And everything but explantion would be generated automatically from the fact the function is used in block
Andrea Moretti
@axyz
Aug 24 2018 13:24
@omni-viral libc::free(memory as *mut libc::c_void); seems to do the job
Zakarum
@omni-viral
Aug 24 2018 13:24
:+1:
Andrea Moretti
@axyz
Aug 24 2018 13:26
how are safety notes parsed? Should I write something like this?
let vector: Vec<u8> = unsafe {
            /// # Safety
            /// memory needs to be freed manually
            let mut result_size: usize = 0;
            let memory: *mut u8 = vips::vips_image_write_to_memory(
                self.img,
                &mut result_size as *mut usize,
            ) as *mut u8;
            let slice = slice::from_raw_parts_mut(memory, result_size);
            let vec = slice.to_vec();
            // manually free the memory
            libc::free(memory as *mut libc::c_void);
            vec
        };
Zakarum
@omni-viral
Aug 24 2018 13:58
@axyz They aren't atm
Fantasized
@Fantasized
Aug 24 2018 14:10
@Virtuos86 well, if you have a function like fn somefn()->String {} and I want to return its caller an empty string ( meaning "") , I could just write it as(if supported ) fn somefn()->String { () } which I basically thought would default construct the type String with an empty literal , so am kinda looking for a way where I could use either () or {} or any xyz that can default construct an object in a much simpler way rather than explicitly mentioning empty values fn somefn()->String { String::xxx() }. that's where the whole thing started.
Lyle Mantooth
@IslandUsurper
Aug 24 2018 14:26
@Fantasized , that's what Default::default() is for. I think in this case it's better to be explicit about what you're returning, whether it's 0i8 or "".to_owned().
Like, I still looked up how Default was implemented for numbers and strings because it didn't have to be zero or empty.
Vitaly
@Virtuos86
Aug 24 2018 14:35

@Fantasized OK, just write your own procedural macro:

#![some_magic]
...
fn somefn() -> String { @_@ } // ==> fn somefn() -> String { <String as Default>::default() }
fn another_somefn() -> i32 { @_@ } // ==> fn another_somefn() -> i32 { <i32 as Default>::default() }

:smirk:

Fantasized
@Fantasized
Aug 24 2018 14:45
String / i32 are just an exemplification, Do you think writing macros for every type T would be a good idea? BTW, am OK with Default::default(). I've not looked at the macro part yet.
Vitaly
@Virtuos86
Aug 24 2018 14:51
@Fantasized No-no-no. My hypothetical macro takes any type of return value from a function signature and inserts in a template "<%type% as Default>::default()"
Fantasized
@Fantasized
Aug 24 2018 14:53
I see.
Scott Corbeil
@scorbeil
Aug 24 2018 16:41
I'm sorry for the silly question, but I've been away from Rust for a bit and struggling to settle back in. How do I important a crate that is a "sub-crate" of another? So crate Foo's cargo manifest dir has a subdir with another crate, Bar. In Foo, there is a file, lib.rs, which includes the line "pub extern crate Bar as Alias;"
Import*
Jan Hlavatý
@hlavaatch
Aug 24 2018 17:31
I guess you extern crate the outer as usual, and use the module inside
you can specify local crate path with the oath attribute but that will not work with crates from crates.io
Jan Hlavatý
@hlavaatch
Aug 24 2018 17:40
you probably want module not a "sub-crate"
Scott Corbeil
@scorbeil
Aug 24 2018 17:58
I don't have a choice :). Not my crate
Michal 'vorner' Vaner
@vorner
Aug 24 2018 18:00
Doesn't use that_crate::Alias::something work (only guessing)?
Michal 'vorner' Vaner
@vorner
Aug 24 2018 18:06
What is the status of this one? It seems to be documented to act that way and likely rustc actually acts that way, but the stabilization feature is still open ‒ can I safely rely on the drop order in practice? rust-lang/rust#43034
Vitaly
@Virtuos86
Aug 24 2018 18:43

I'm sorry for the silly question, but I've been away from Rust for a bit and struggling to settle back in. How do I important a crate that is a "sub-crate" of another? So crate Foo's cargo manifest dir has a subdir with another crate, Bar. In Foo, there is a file, lib.rs, which includes the line "pub extern crate Bar as Alias;"

Foo's Cargo.toml:

[dependencies]
Bar = { path = "./Bar" }

In Foo's lib.rs:

pub extern crate Bar as Alias;
...
Scott Corbeil
@scorbeil
Aug 24 2018 19:55
Yes, @Virtuos86 , that's the situation. I'm wondering how I import Bar in some third crate, Baz, which is unrelated to the first two
Lyle Mantooth
@IslandUsurper
Aug 24 2018 19:59
Most likely:
extern crate Foo;

use Foo::Alias;
Or, use whichever specific parts of Bar you need through Foo::Alias.
Could even undo the alias as use Foo::Alias as Bar;
Scott Corbeil
@scorbeil
Aug 24 2018 21:40
Thanks. I'll try that when I'm back in front of it