rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.74k stars 256 forks source link

`Exception Safety` section: `Hole::get` probably doesn't have to be `unsafe` #426

Closed frozenspider closed 8 months ago

frozenspider commented 8 months ago

https://doc.rust-lang.org/nomicon/exception-safety.html

struct Hole<'a, T: 'a> {
    data: &'a mut [T],
    /// `elt` is always `Some` from new until drop.
    elt: Option<T>,
    pos: usize,
}

impl<'a, T> Hole<'a, T> {
    // ...

    unsafe fn get(&self, index: usize) -> &T { &self.data[index] }

    // ...
}

Why is get here marked as unsafe? It doesn't seem to be performing anything particularly unsafe, any more so than regular safe code might do. Rust compiler also doesn't require it to be unsafe.

JohnTitor commented 8 months ago

I guess it was just missed when writing, a PR is welcome!