rust-osdev / volatile

Apache License 2.0
71 stars 19 forks source link

Make Volatile derive Copy. #6

Closed cedws closed 5 years ago

cedws commented 5 years ago

Hi there. This PR makes the Volatile struct derive Copy. I'm not sure whether this will have any negative or unintended consequences.

To clarify what this is needed for, I am currently looking to improve some of the VGA text buffer code in Philipp Oppermann's blog_os. This PR allows an optimisation to be made for which I will send a PR there too.

phil-opp commented 5 years ago

Hi, thanks for the PR! The problem with implementing Copy for Volatile is that it makes accidental copies possible. Most of the time, a volatile refers to a specific memory location, so it does not make much sense to copy it e.g. to the stack. See https://github.com/embed-rs/volatile/pull/5#issuecomment-353783786 for an example.

I hope that https://github.com/rust-lang/rust/pull/53645 is merged soon. It implements const generics, which means that we could create a VolatileArray<T, const N> type, which could solve the initialization problem easily through a VolatileArray::new(array: [T; N]) method.

cedws commented 5 years ago

Ah I see, I was trying to do something similar with [Volatile::new(_); N];. It seems VolatileArray would be more optimal. Thanks a lot, I have a few other changes for the kernel that you might like.