Skip to content

Commit

Permalink
Merge #134
Browse files Browse the repository at this point in the history
134: Make Fetch cloneable r=torkleyy a=Marwes

Working on allowing [gluon](https://github.com/gluon-lang/gluon) to work with specs/shred and ended up needing to clone some `Fetch<EntitiesRes>` values. I don't know fully how specs/shred work internally but this should be equivalent to doing multiple `fetch` operations and should therefore be sound.

Co-authored-by: Markus Westerlind <[email protected]>
  • Loading branch information
bors[bot] and Marwes committed Apr 22, 2019
2 parents 6328ba8 + c997d99 commit c10d87a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ impl<'a, T> Drop for Ref<'a, T> {
}
}

impl<'a, T> Clone for Ref<'a, T> {
fn clone(&self) -> Self {
self.flag.fetch_add(1, Ordering::Release);
Ref {
flag: self.flag,
value: self.value,
}
}
}

/// A mutable reference to data in a `TrustCell`.
///
/// Access the value via `std::ops::DerefMut` (e.g. `*val`)
Expand Down Expand Up @@ -222,6 +232,16 @@ mod tests {
assert_eq!(10, *a + *b);
}

#[test]
fn allow_clone_reads() {
let cell: TrustCell<_> = TrustCell::new(5);

let a = cell.borrow();
let b = a.clone();

assert_eq!(10, *a + *b);
}

#[test]
fn allow_single_write() {
let cell: TrustCell<_> = TrustCell::new(5);
Expand Down Expand Up @@ -295,4 +315,15 @@ mod tests {

assert!(cell.try_borrow_mut().is_err());
}

#[test]
fn cloned_borrow_does_not_allow_write() {
let cell: TrustCell<_> = TrustCell::new(5);

let a = cell.borrow();
let _b = a.clone();
drop(a);

assert!(cell.try_borrow_mut().is_err());
}
}
9 changes: 9 additions & 0 deletions src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ where
}
}

impl<'a, T> Clone for Fetch<'a, T> {
fn clone(&self) -> Self {
Fetch {
inner: self.inner.clone(),
phantom: PhantomData,
}
}
}

/// Allows to fetch a resource in a system mutably.
///
/// If the resource isn't strictly required, you should use
Expand Down

0 comments on commit c10d87a

Please sign in to comment.