Uh oh!
There was an error while loading. Please reload this page.
Add Copy derives - #5
Conversation
phil-opp
commented
Dec 24, 2017
I think we wanted to avoid accidental copies. A volatile is normally bound to a specific memory location (e.g. the VGA buffer or memory mapped registers). You can copy the current value, but copying the volatile itself to a stack variable does not make much sense, since there are no side-effects for stack variables. For example, consider the following code: // set to true by hardware if button is currently pressedstaticBUTTON:&'staticmutVolatile<bool> = …;// correctwhile !BUTTON.read(){}// wait until button is pressed// wronglet b = *BUTTON;while !b.read(){}// copy the current button value to the stack and wait until it changesYes, the explicit dereference ( The initialization problem is unfortunate. You could try to use the Default trait, but it only works for fixed array sizes. Maybe Rust will be able to allow initialization with non-Copy types when we get the new constant evaluator. Until then, you can use the array_init crate. |
I've been improving my VBA buffer logic and found something I can't do.
This is inconvenient, and there doesn't appear to be any good reason why Volatile doesn't implement Copy - the contents are required to by trait constraint - so I made a quick pull request.