Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it safe to implement a rwlock_filter_map #298

Open
lucassardois opened this issue Sep 20, 2021 · 0 comments
Open

Is it safe to implement a rwlock_filter_map #298

lucassardois opened this issue Sep 20, 2021 · 0 comments

Comments

@lucassardois
Copy link

lucassardois commented Sep 20, 2021

This crate: https://docs.rs/ref_filter_map/1.0.1/ref_filter_map/ allow to deal with guard and options using an unsafe operation.
The core of the crate is this code:

pub fn ref_filter_map<
    T: ?Sized,
    U: ?Sized,
    F: FnOnce(&T) -> Option<&U>
>(orig: Ref<T>, f: F) -> Option<Ref<U>> {
    f(&orig)
        .map(|new| new as *const U)
        .map(|raw| Ref::map(orig, |_| unsafe { &*raw }))
}

Would translating the previous code snipped to RwLock works safely?

pub fn rwlock_filter_map<
    T: ?Sized,
    U: ?Sized,
    F: FnOnce(&T) -> Option<&U>
>(orig: MappedRwLockReadGuard<T>, f: F) -> Option<MappedRwLockReadGuard<U>> {
    f(&orig)
        .map(|new| new as *const U)
        .map(|raw| MappedRwLockReadGuard::map(orig, |_| unsafe { &*raw }))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant