These are chat archives for rust-lang/rust

14th
Jan 2018
dunnousername
@dunnousername
Jan 14 2018 00:02
Is there a way to get vector-like types in a freestanding environment? I do not have malloc implemented yet, but I could do that. I'm currently passing around mutable byte arrays and 12-long tuples, and I need the String type for a serial driver. Can I get String in a crate without porting libstd?
Steve Klabnik
@steveklabnik
Jan 14 2018 00:03
yes
string is in liballoc
so, if you get it working, you don't need all of std
if you implement malloc, in my understanding, liballoc is easy, since that's the only real dependency it needs
well, an allocator generally, not only malloc
that said i have not done so myself
dunnousername
@dunnousername
Jan 14 2018 00:06
Is Vector in liballoc?
Steve Klabnik
@steveklabnik
Jan 14 2018 00:06
i think so
given that String is a wrapper around Vec
i'd imagine so, if String is
dunnousername
@dunnousername
Jan 14 2018 00:08
I wasn't sure if I was coding wrong when modifying byte arrays in function, I guess you would usually use String to deal with it.
dunnousername
@dunnousername
Jan 14 2018 00:16
@steveklabnik , I'm looking around at the code for paging, and as I understand, I can reserve a 2MiB page for the kernel, and then malloc inside that. Is that how you would do that?
Christopher Andronikos
@candronikos
Jan 14 2018 00:32
@steveklabnik That's definitely interesting... I'll keep an eye out
dunnousername
@dunnousername
Jan 14 2018 00:34
I've found phil-opp's nightly-liballoc crate, but I have no idea how to tell it to use my malloc function
dunnousername
@dunnousername
Jan 14 2018 00:40
I've found https://crates.io/crates/alloc_buddy_simple , which may be useful. Going to check it out
Hans W. Uhlig
@huhlig
Jan 14 2018 03:41
Anyone know what's wrong with rustup lately
warning: tool `rustfmt` is already installed, remove it from `C:\Users\huhlig\.cargo\bin`, then run `rustup update` to have rustup manage this tool. warning: tool `cargo-fmt` is already installed, remove it from `C:\Users\huhlig\.cargo\bin`, then run `rustup update` to have rustup manage this tool.
I've deleted those files multiple times, I've even completely wiped the 3 rustup directories and reinstalled
dunnousername
@dunnousername
Jan 14 2018 04:08
Hey, I'm now trying to re-implement Box<T>, and I get weird errors when trying to impl Clone for Box<T>. Is there any way to implement a concrete trait for a generic struct?
Same with Drop, too. I'm trying to deep drop/clone through a raw pointer of type *mut T, and Box<T> contains a property called ptr which stores the raw pointer. I want it to bee free()'d and malloc()'d on Drop and Clone. I'm also making a vector, which is just an array of Box<T>'s that gets realloc()'d every time a resize happens.
dunnousername
@dunnousername
Jan 14 2018 04:13
@huhlig I'm not a windows expert, but I know that sometimes on linux the shell thinks that deleted executables in $PATH still exist until you restart the terminal. This may be a stupid answer, but try restarting the terminal? Otherwise, I have no idea...
dunnousername
@dunnousername
Jan 14 2018 04:25
Okay, now I'm really confused:

error[E0277]: the trait bound `T: core::clone::Clone` is not satisfied
  --> src/mem/boks.rs:21:9
   |
21 | impl<T> Drop for Box<T> {
   |         ^^^^ the trait `core::clone::Clone` is not implemented for `T`
   |
   = help: consider adding a `where T: core::clone::Clone` bound
   = note: required by `mem::boks::Box`
It says that everywhere I impl<T> Box or impl<T> XYZ for Box<T>
Do I have to somehow implement the Clone trait for the Drop trait? That doesn't really make sense...
red75prime
@red75prime
Jan 14 2018 06:50
@dunnousername You probably added T: Clone constraint in your reimplementation of Box<T>. note: required by mem::boks::Box
Murali
@mmrath
Jan 14 2018 09:28
How do I link to a method from different crate from my crates's doc?
dunnousername
@dunnousername
Jan 14 2018 17:21
I can't have Clone and Drop?
I think I solved that one, now I'm implementing Vec<T>. But, I end up making every function and impl of every trait where T: Clone + ?Sized, and that seems wrong, as it is a lot of traits. Now I'm getting
error[E0046]: not all trait items implemented, missing: `Output`
   --> src/mem/vec.rs:145:1
    |
145 | / impl<T> Add<Vec<T>> for Vec<T>
146 | |     where T: Clone + ?Sized {
147 | |     type Output: Vec<T>
148 | |         where T: Clone + ?Sized;
...   |
157 | |     }
158 | | }
    | |_^ missing `Output` in implementation
    |
    = note: `Output` from trait: `type Output;`
And I clearly have type Output...
Denis Lisov
@tanriol
Jan 14 2018 17:42
type Output = Vec<T>
Also, do you really need Clone?
dunnousername
@dunnousername
Jan 14 2018 17:58
It makes a lot of things easier for dealing with, for example, Iterator. However, is it possible for an Iterator to return a reference? Is that normal?
When I call Vec.iter() I clone the Vec<T> into VecIter<T> and iterate over that, so I don't have to deal with mutability and such.
Denis Lisov
@tanriol
Jan 14 2018 18:04
The standard slice iterator returns a reference to the element.
dunnousername
@dunnousername
Jan 14 2018 18:05
I'll probably make Vec.iter() keep a reference of Vec.
dunnousername
@dunnousername
Jan 14 2018 18:22
I have it implemented so that T is ?Sized, and in the struct is a C-like array pointer (*mut Box<T>, even though it is more than one. I'm using the raw pointer offset() method). Is this bad?
pub struct Vec<T>
    where T: Clone + ?Sized {
    arr: *mut Box<T>,
    len: usize,
}
Do raw pointers count as mutable references? In that case, I could not immutably borrow self.
dunnousername
@dunnousername
Jan 14 2018 18:29
Actually, because my Box<T> only stores a pointer to the data, I can clone the vector without it being huge.
dunnousername
@dunnousername
Jan 14 2018 19:28
I'm trying to use core::fmt to print a Debug of a struct over a serial port. I'm not sure how I would use this, as everything requires macros or stdlib, but I'm on a freestanding bare-metal environment.
dunnousername
@dunnousername
Jan 14 2018 21:25
Is it possible for code without while loops or loops, as well as without recursion, to call other functions that return, but never return? I really have no idea what is happening here... a lot of those functions are unsafe, but that is unavoidable.
I'm just matching values and assigning to structs, as well as computing a checksum (just adding numbers and checking if they equal zero).
Tom Watson
@FridgeSeal
Jan 14 2018 21:31
Any chance you're operating on an infinite iterator or something? Alternatively could one of your unsafe functions not be returning properly?
dunnousername
@dunnousername
Jan 14 2018 21:33
It gets to a certain point, but then I'm using no iterators after that point AFAIK. Something that may be causing it, though, is that I'm adding like 10 byte values together to a u8, so it is supposed to overflow. Then I return that.
let checksum: u8 = (
        (x.signature & 0xff000000) >> 24 +
        (x.signature & 0x00ff0000) >> 16 +
        (x.signature & 0x0000ff00) >>  8 +
        (x.signature & 0x000000ff) >>  0 +
        (x.mpconfigptr & 0xff000000) >> 24 +
        (x.mpconfigptr & 0x00ff0000) >> 16 +
        (x.mpconfigptr & 0x0000ff00) >>  8 +
        (x.mpconfigptr & 0x000000ff) >>  0
        )
[...]
x.signature and x.mpconfigptr are u32's

Then, I do

    serial::writes_serial(serial::COM1, "Checksum zero?\n");
    if checksum == 0 {
        serial::writes_serial(serial::COM1, "Yes!\n");
    } else {
        serial::writes_serial(serial::COM1, "No\n");
    }

But nothing appears in the serial terminal (other debug messages do print to the serial terminal, though)

This message was deleted
Tom Watson
@FridgeSeal
Jan 14 2018 21:39
hmmm, I'm out of my depth now sorry, I'm still super new to Rust
dunnousername
@dunnousername
Jan 14 2018 21:39
Actually, I am too, so I'm probably doing something others may find obvious...
Noah Weninger
@nwoeanhinnogaehr
Jan 14 2018 21:59
@dunnousername Not sure how it works on your platform, but integer overflow in debug mode is checked, meaning you checksum computation might panic. Maybe try std::num::Wrapping?
dunnousername
@dunnousername
Jan 14 2018 22:39
@nwoeanhinnogaehr I'm in debug mode, that is so helpful. I understand that is a good safety feature, but I don't really like it coming from languages such as C. Is there a crate-level option?
And, could I do, for example,
let checksum: u8 = (
        Wrapping(x.signature & 0xff000000) >> 24 +
        (x.signature & 0x00ff0000) >> 16 +
        (x.signature & 0x0000ff00) >>  8 +
        (x.signature & 0x000000ff) >>  0 +
        (x.mpconfigptr & 0xff000000) >> 24 +
        (x.mpconfigptr & 0x00ff0000) >> 16 +
        (x.mpconfigptr & 0x0000ff00) >>  8 +
        (x.mpconfigptr & 0x000000ff) >>  0
        )
[...]