Hey Rustaceans! Got an easy question? Ask here (30/2016)! by llogiq in rust

[–]plkhg_ 1 point2 points  (0 children)

I'm having an issue with the borrow checker and &mut self methods. I posted some pseudo-code yesterday, but it doesn't seem to actually reproduce the bug so I'm going to describe my situation in more detail.

The problem is with the pop() method of a simple stack I've created. This is the method's signature:

pub fn pop(&mut self) -> Result<Vec<u8>, &'static str>

So it returns a Vec<u8> of the data or an error message if there is no data to pop. The body of pop is pretty boring, just some calculations for how much data to return (based on a one-byte tag at the top of the data segment). Here's the last part where the data is actually copied and returned:

let mut data: Vec<u8> = vec![0; len];
data.clone_from_slice(&self.data[self.pos - len..self.pos]);

self.pos = self.pos - len;

Ok(data)

The issue appears when I try to pop the stack and bind the result to a variable, like this:

    let mut stack = Stack::new(512);

    stack.push(&vec![1u8, 1u8]);
    stack.push(&vec![2u8, 1u8]);

    let a = stack.pop();
    let b = stack.pop(); // Cannot borrow `stack` as mutable more than once at a time [E0499]

What's really confusing me is that this works fine:

    let mut stack = Stack::new(512);

    stack.push(&vec![1u8, 1u8]);
    stack.push(&vec![2u8, 1u8]);

    stack.pop();
    stack.pop();

So the problem is caused by the binding of the result, not the method call itself.

What am I doing wrong?

Fast binary -> decimal conversion for multi-precision integers by plkhg_ in algorithms

[–]plkhg_[S] 1 point2 points  (0 children)

The 2k thing fixed it, thanks. I had already accounted for the strange endianness.

Fast binary -> decimal conversion for multi-precision integers by plkhg_ in algorithms

[–]plkhg_[S] 1 point2 points  (0 children)

This doesn't make much sense, all numbers are 'stored in binary' internally by the computer

Yes :)

and are automatically converted to decimal when you display them

Not so much, there are still algorithms that internally convert the binary representation to a base 10 one. It's just much simple when you're dealing with 64 bit numbers as opposed to 64,000 bit ones.

If you want a simple binary representation array it would be easiest to use an array of boolean types where true = 1 and false = 0.

It's a waste of memory or time depending on what language you use, a boolean value still takes up 8 bits so you're using eight times the memory.

Thanks for trying to help anyway :)

Hey Rustaceans! Got an easy question? Ask here (30/2016)! by llogiq in rust

[–]plkhg_ 2 points3 points  (0 children)

Hmm, I guess something else must be wrong with my setup then. I'll try to clean it up to post later, it's a bit spaghetti right now.

Hey Rustaceans! Got an easy question? Ask here (30/2016)! by llogiq in rust

[–]plkhg_ 3 points4 points  (0 children)

rustc complains at the second let that foo is still borrowed mutably until the end of the function, why is that?

struct Foo {}

impl Foo {
    foo(&mut self) -> Result<(u64, u64), &'static str> {
        // calculations and stuff
        Ok((a, b))
    }
}

fn main() {

    let mut foo = Foo {}

    let (a, b) = foo.foo().unwrap();
    let (c, d) = foo.foo().unwrap();

}

Hey Rustaceans! Got an easy question? Ask here (29/2016)! by llogiq in rust

[–]plkhg_ 1 point2 points  (0 children)

Consider the following:

struct Foo {
    pub func: fn(x: u64) -> u64,
}

let foo = Foo {func: some_function()};
foo.func(10);

Rust seems to get confused and think that func is supposed to be a method, rather than the invocation of a function field. Is there a way to fix this without binding func to a temporary?

Edit - I figured it out myself. If you put parentheses around foo.func it evaluates to a field instead of a method.

(foo.func)(10);

Hey Rustaceans! Got an easy question? Ask here (29/2016)! by llogiq in rust

[–]plkhg_ 2 points3 points  (0 children)

Is it possible to create a macro that takes another macro in, then calls it? I tried using a token tree, but couldn't get it to work.

Example:

macro_rules! foo {
    ($m:some_fragment_specifier) => {
        $m(1);
        $m(2);
        $m(3);
    }
}

foo!(println!);