Skip to content
Snippets Groups Projects
Commit 0faba12c authored by Lukas Markeffsky's avatar Lukas Markeffsky
Browse files

bonus asserts + test

parent 35cdd195
Branches
No related merge requests found
...@@ -135,6 +135,7 @@ pub struct Unclaimed<T> { ...@@ -135,6 +135,7 @@ pub struct Unclaimed<T> {
impl<T> Unclaimed<T> { impl<T> Unclaimed<T> {
pub fn claim(self, value: T) -> Xrc<T> { pub fn claim(self, value: T) -> Xrc<T> {
unsafe { unsafe {
debug_assert_eq!((*self.ptr.as_ptr()).state.get(), STATE_UNCLAIMED);
(*self.ptr.as_ptr()).state.set(STATE_SHARED_INIT); (*self.ptr.as_ptr()).state.set(STATE_SHARED_INIT);
} }
...@@ -163,6 +164,7 @@ impl<T: ?Sized> Drop for Xrc<T> { ...@@ -163,6 +164,7 @@ impl<T: ?Sized> Drop for Xrc<T> {
let state_ref = unsafe { &(*root.as_ptr()).state }; let state_ref = unsafe { &(*root.as_ptr()).state };
let state = state_ref.get(); let state = state_ref.get();
debug_assert!(state <= STATE_SHARED_MAX);
if state != STATE_SHARED_INIT { if state != STATE_SHARED_INIT {
state_ref.set(state - 1); state_ref.set(state - 1);
return; return;
...@@ -184,6 +186,7 @@ impl<T> Clone for Xrc<T> { ...@@ -184,6 +186,7 @@ impl<T> Clone for Xrc<T> {
let state_ref = unsafe { &(*root.as_ptr()).state }; let state_ref = unsafe { &(*root.as_ptr()).state };
let state = state_ref.get(); let state = state_ref.get();
debug_assert!(state <= STATE_SHARED_MAX);
if state == STATE_SHARED_MAX { if state == STATE_SHARED_MAX {
panic!("ref count overflow"); panic!("ref count overflow");
} }
...@@ -243,6 +246,7 @@ mod tests { ...@@ -243,6 +246,7 @@ mod tests {
use std::pin::pin; use std::pin::pin;
use std::ptr; use std::ptr;
use std::rc::Rc; use std::rc::Rc;
use std::panic::{self, AssertUnwindSafe};
fn iter_pinned_mut<T>(slice: Pin<&mut [T]>) -> impl Iterator<Item = Pin<&mut T>> { fn iter_pinned_mut<T>(slice: Pin<&mut [T]>) -> impl Iterator<Item = Pin<&mut T>> {
unsafe { unsafe {
...@@ -260,6 +264,21 @@ mod tests { ...@@ -260,6 +264,21 @@ mod tests {
assert_eq!(xrc.0, 42); assert_eq!(xrc.0, 42);
} }
#[test]
fn unpin_hack() {
let mut root = pin!(Root::new());
let xrc = root.as_mut().unclaimed().claim(NoReclaim(42));
let res = panic::catch_unwind(AssertUnwindSafe(|| {
// This must not create an unique borrow ...
let _ = root.unclaimed();
}));
assert!(res.is_err());
// ... to not invalidate this shared read-only borrow.
assert_eq!(xrc.0, 42);
}
#[test] #[test]
#[ignore = "will abort"] #[ignore = "will abort"]
fn abort() { fn abort() {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment