Skip to content

Commit

Permalink
refactor: use unsafe for get_mut from ref
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Aug 26, 2023
1 parent 4385992 commit ce92ffd
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions y-octo/src/doc/common/somr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<T> Somr<T> {
inner.data.as_mut().map(|x| x.get_mut())
}

pub fn get_mut_from_ref(&self) -> Option<InnerRefMut<T>> {
pub unsafe fn get_mut_from_ref(&self) -> Option<InnerRefMut<T>> {
if !self.is_owned() || self.dangling() {
return None;
}
Expand All @@ -141,7 +141,6 @@ impl<T> Somr<T> {
inner.data_mut()
}

#[allow(clippy::mut_from_ref)]
pub unsafe fn get_mut_unchecked(&self) -> InnerRefMut<'_, T> {
if self.dangling() {
panic!("Try to visit Somr data that has already been dropped.")
Expand Down Expand Up @@ -417,7 +416,7 @@ mod tests {

// only owner can mut ref
assert!(five_ref.get().is_some());
assert!(five_ref.get_mut_from_ref().is_none());
assert!(unsafe { five_ref.get_mut_from_ref() }.is_none());

drop(five);
});
Expand Down Expand Up @@ -490,7 +489,9 @@ mod tests {
fn test_inner_mut() {
let five = Somr::new(5);
fn add(a: &Somr<i32>, b: &Somr<i32>) {
a.get_mut_from_ref().map(|mut x| *x += *b.get().unwrap()).unwrap();
unsafe { a.get_mut_from_ref() }
.map(|mut x| *x += *b.get().unwrap())
.unwrap();
}

add(&five, &five);
Expand Down

0 comments on commit ce92ffd

Please sign in to comment.