[][src]Function kai::promote_then

pub unsafe fn promote_then<T, R, F>(var: &T, f: F) -> R where
    F: FnOnce(&mut T) -> R, 

Temporarily gain access to an immutable reference as mutable

This function attempts to make promoting a reference to be mutable slightly less unsafe. It does this by wrapping access to the mutable reference in a closure, thus bounding the lifetime. This allows the compiler to reject certain unsafe behaviors and misuse of the mutable reference. That being said, there are probably still a ton of things that could go wrong, so this function is still marked unsafe. If you are someone who knows the specific ways that using this function could still cause undefined behvaior, please let me know by emailing me or opening an issue.

Example

use kai::*;

let v = vec![5];
let x = unsafe { promote_then(&v, |v| v.remove(0)) };
assert!(v.is_empty());
assert_eq!(5, x);