amethyst / shred

Shared resource dispatcher
Apache License 2.0
234 stars 66 forks source link

Replace unsafe code with safer variant #185

Closed cynecx closed 2 years ago

cynecx commented 4 years ago

I've been digging in some of the internals of shred with emphasis on unsafe code and I've noticed this particular method:

https://github.com/amethyst/shred/blob/c1bfcda62abc590a77a4d2905dbcdccadfefd378/src/cell.rs#L91-L112

I can't see why one would use unsafe here, if I am not missing anything crucial here, this could be replaced with a completely safe alternative:

    pub fn map<U, F>(self, f: F) -> Ref<'a, U>
    where
        F: FnOnce(&T) -> &U,
        U: ?Sized,
    {
        let val = Ref {
            flag: self.flag,
            value: f(self.value),
        };

        std::mem::forget(self);

        val
    }

Playground