Re: [rust-dev] Mutability and borrowing
Going by the suggested solution of using Atomic Reference Counters (std::arc) for sharing immutable data across different tasks, I would presume that the most generic solution would be to use the arc module. You can see an example of this in action here : https://github.com/smadhueagle/rustlings/blob/master/borrow.rs Madhu From: hata...@gmail.com Date: Sat, 1 Jun 2013 22:30:35 -0700 To: abhijeet.ga...@gmail.com CC: rust-dev@mozilla.org Subject: Re: [rust-dev] Mutability and borrowing True. I suppose I meant generic and memory efficient at the same time. I guess it shouldn't be too much of a hassle to implement the specialized case of this function if memory is absolutely critical. Thanks -- Ziad On Sat, Jun 1, 2013 at 10:03 PM, Abhijeet Gaiha abhijeet.ga...@gmail.com wrote: The 'copy' parameter is the most generic way for the client to handle this situation. -- Abhijeet Gaihahttp://about.me/abhijeet.gaiha On Sunday, 2 June 2013 at 10:32 AM, Ziad Hatahet wrote: Thanks everyone. I actually thought about the two suggestions before posting. I thought there might be some common paradigm for this in the language though. So I take it that implementing a += operator overload function would not have a generic way to handle the case where the same parameter is passed on both sides? -- Ziad On Sat, Jun 1, 2013 at 9:42 PM, Tim Chevalier catamorph...@gmail.com wrote: On Sat, Jun 1, 2013 at 9:33 PM, Ziad Hatahet hata...@gmail.com wrote: I have the following function: fn add_equal(x: mut Complex, y: Complex) { x.real += y.real; x.imag += y.imag; } Calling the function with the same variable being passed to both arguments (i.e. add_equal(mut c, c)), results in the compile error: error: cannot borrow `c` as immutable because it is also borrowed as mutable I am guessing this is to avoid aliasing issues? What is the way around this? You can copy y instead of passing a reference to it: fn add_equal(x: mut Complex, y: Complex) { ... Of course, that means that at the call site, you will have to write something like add_equal(mut c, copy c). Unless you want to write a function that just takes one argument and doubles it, like Abhijeet suggested, I don't know of another way around this. Cheers, Tim -- Tim Chevalier * http://catamorphism.org/ * Often in error, never in doubt Not a riot, it's a rebellion. -- Boots Riley Attention Bros and Trolls: When I call out your spew, I'm not angry, I'm defiant. -- Reg Braithwaite ___Rust-dev mailing listrust-...@mozilla.org https://mail.mozilla.org/listinfo/rust-dev ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Mutability and borrowing
On Sun, Jun 2, 2013 at 12:33 AM, Ziad Hatahet hata...@gmail.com wrote: I have the following function: fn add_equal(x: mut Complex, y: Complex) { x.real += y.real; x.imag += y.imag; } Calling the function with the same variable being passed to both arguments (i.e. add_equal(mut c, c)), results in the compile error: error: cannot borrow `c` as immutable because it is also borrowed as mutable I am guessing this is to avoid aliasing issues? What is the way around this? Thanks -- Ziad You can currently use `const Complex` for the second parameter, but it may or may not be removed in the future. At the very least it will probably be renamed. An `` pointer guarantees that the value it points to is immutable, but `const` is allowed to alias `mut` since the compiler restricts it much more. Ideally you would use a separate function for doubling the components of a value and adding two together. ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Mutability and borrowing
On Sun, Jun 2, 2013 at 6:56 AM, Daniel Micay danielmi...@gmail.com wrote: You can currently use `const Complex` for the second parameter, but it may or may not be removed in the future. At the very least it will probably be renamed. Excellent. Thanks all for your replies :) -- Ziad ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
[rust-dev] Mutability and borrowing
I have the following function: fn add_equal(x: mut Complex, y: Complex) { x.real += y.real; x.imag += y.imag; } Calling the function with the same variable being passed to both arguments (i.e. add_equal(mut c, c)), results in the compile error: error: cannot borrow `c` as immutable because it is also borrowed as mutable I am guessing this is to avoid aliasing issues? What is the way around this? Thanks -- Ziad ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Mutability and borrowing
Trying to concurrently borrow a value twice is never going to work. You could write a function like double for such a situation. -- Abhijeet Gaiha http://about.me/abhijeet.gaiha On Sunday, 2 June 2013 at 10:03 AM, Ziad Hatahet wrote: I have the following function: fn add_equal(x: mut Complex, y: Complex) { x.real += y.real; x.imag += y.imag; } Calling the function with the same variable being passed to both arguments (i.e. add_equal(mut c, c)), results in the compile error: error: cannot borrow `c` as immutable because it is also borrowed as mutable I am guessing this is to avoid aliasing issues? What is the way around this? Thanks -- Ziad ___ Rust-dev mailing list Rust-dev@mozilla.org (mailto:Rust-dev@mozilla.org) https://mail.mozilla.org/listinfo/rust-dev ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Mutability and borrowing
On Sat, Jun 1, 2013 at 9:33 PM, Ziad Hatahet hata...@gmail.com wrote: I have the following function: fn add_equal(x: mut Complex, y: Complex) { x.real += y.real; x.imag += y.imag; } Calling the function with the same variable being passed to both arguments (i.e. add_equal(mut c, c)), results in the compile error: error: cannot borrow `c` as immutable because it is also borrowed as mutable I am guessing this is to avoid aliasing issues? What is the way around this? You can copy y instead of passing a reference to it: fn add_equal(x: mut Complex, y: Complex) { ... Of course, that means that at the call site, you will have to write something like add_equal(mut c, copy c). Unless you want to write a function that just takes one argument and doubles it, like Abhijeet suggested, I don't know of another way around this. Cheers, Tim -- Tim Chevalier * http://catamorphism.org/ * Often in error, never in doubt Not a riot, it's a rebellion. -- Boots Riley Attention Bros and Trolls: When I call out your spew, I'm not angry, I'm defiant. -- Reg Braithwaite ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] Mutability and borrowing
Thanks everyone. I actually thought about the two suggestions before posting. I thought there might be some common paradigm for this in the language though. So I take it that implementing a += operator overload function would not have a generic way to handle the case where the same parameter is passed on both sides? -- Ziad On Sat, Jun 1, 2013 at 9:42 PM, Tim Chevalier catamorph...@gmail.comwrote: On Sat, Jun 1, 2013 at 9:33 PM, Ziad Hatahet hata...@gmail.com wrote: I have the following function: fn add_equal(x: mut Complex, y: Complex) { x.real += y.real; x.imag += y.imag; } Calling the function with the same variable being passed to both arguments (i.e. add_equal(mut c, c)), results in the compile error: error: cannot borrow `c` as immutable because it is also borrowed as mutable I am guessing this is to avoid aliasing issues? What is the way around this? You can copy y instead of passing a reference to it: fn add_equal(x: mut Complex, y: Complex) { ... Of course, that means that at the call site, you will have to write something like add_equal(mut c, copy c). Unless you want to write a function that just takes one argument and doubles it, like Abhijeet suggested, I don't know of another way around this. Cheers, Tim -- Tim Chevalier * http://catamorphism.org/ * Often in error, never in doubt Not a riot, it's a rebellion. -- Boots Riley Attention Bros and Trolls: When I call out your spew, I'm not angry, I'm defiant. -- Reg Braithwaite ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev