Skip to content

Commit

Permalink
Merge pull request #14 from bluss/fix-join-cover
Browse files Browse the repository at this point in the history
Fix bug in .join_cover() and bug in ContiguousMut
  • Loading branch information
bluss authored Sep 7, 2019
2 parents d6fcd7a + 2aa7b97 commit e4eb6e6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/container_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ pub unsafe trait GetUncheckedMut : GetUnchecked {
}

pub unsafe trait ContiguousMut : Contiguous {
fn begin_mut(&self) -> *mut Self::Item {
self.begin() as _
}
fn end_mut(&self) -> *mut Self::Item {
self.end() as _
}
fn begin_mut(&mut self) -> *mut Self::Item;
fn end_mut(&mut self) -> *mut Self::Item;
fn as_mut_slice(&mut self) -> &mut [Self::Item];
}

Expand All @@ -52,6 +48,8 @@ unsafe impl<'a, C: ?Sized> Trustworthy for &'a mut C
unsafe impl<'a, C: ?Sized> ContiguousMut for &'a mut C
where C: ContiguousMut
{
fn begin_mut(&mut self) -> *mut Self::Item { (**self).begin_mut() }
fn end_mut(&mut self) -> *mut Self::Item { (**self).end_mut() }
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
(**self).as_mut_slice()
}
Expand Down Expand Up @@ -115,6 +113,14 @@ unsafe impl<T> Trustworthy for [T] {
}

unsafe impl<T> ContiguousMut for [T] {
fn begin_mut(&mut self) -> *mut Self::Item {
self.as_mut_ptr()
}
fn end_mut(&mut self) -> *mut Self::Item {
unsafe {
self.begin_mut().add(self.len())
}
}
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
self
}
Expand All @@ -138,7 +144,7 @@ unsafe impl<T> Contiguous for [T] {
}
fn end(&self) -> *const Self::Item {
unsafe {
self.begin().offset(self.len() as isize)
self.begin().add(self.len())
}
}
fn as_slice(&self) -> &[Self::Item] {
Expand All @@ -155,6 +161,8 @@ mod vec_impls {
}

unsafe impl<T> ContiguousMut for Vec<T> {
fn begin_mut(&mut self) -> *mut Self::Item { (**self).begin_mut() }
fn end_mut(&mut self) -> *mut Self::Item { (**self).end_mut() }
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
self
}
Expand Down
42 changes: 41 additions & 1 deletion src/indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,28 @@ impl<'id, P> Range<'id, P> {
}

/// Extend the range to the end of `other`, including any space in between
pub fn join_cover<Q>(&self, other: Range<'id, Q>) -> Range<'id, <(P, Q) as ProofAdd>::Sum>
///
///
/// ```compile-fail
/// // compile-fail only enabled in 2018 edition
/// // Bug from https://github.com/bluss/indexing/issues/12
/// use indexing::scope;
///
/// let array = [0, 1, 2, 3, 4, 5];
/// scope(&array[..], |arr| {
/// let left = arr.vet_range(0..2).unwrap();
/// let left = left.nonempty().unwrap();
/// let (_, right) = arr.range().frontiers();

/// let joined = right.join_cover(left);
/// let ix = joined.first();
/// dbg!(arr[ix]); //~ ERROR Can't index by ix, because it's an edge index
/// });
/// ```
// Proof P: Extends at least as far as self, not necessarily using any part
// of other
//
pub fn join_cover<Q>(&self, other: Range<'id, Q>) -> Range<'id, P>
where (P, Q): ProofAdd,
{
let end = cmp::max(self.end, other.end);
Expand All @@ -188,6 +209,7 @@ impl<'id, P> Range<'id, P> {
}

/// Extend the range to start and end of `other`, including any space in between
// Proof Sum of P and Q: Covers the union, so must be nonempty if any are
pub fn join_cover_both<Q>(&self, other: Range<'id, Q>) -> Range<'id, <(P, Q) as ProofAdd>::Sum>
where (P, Q): ProofAdd,
{
Expand Down Expand Up @@ -563,3 +585,21 @@ fn test_frac_step() {
assert_eq!(f.next(), None);
}


#[test]
fn test_join_cover() {
use scope;

// Bug from https://github.com/bluss/indexing/issues/12
let array = [0, 1, 2, 3, 4, 5];
scope(&array[..], |arr| {
let left = arr.vet_range(0..2).unwrap();
let left = left.nonempty().unwrap();
let (_, right) = arr.range().frontiers();

let joined = right.join_cover(left);
let ix = joined.first();
assert!(!ix.nonempty_proof());
ix.integer()
});
}
28 changes: 28 additions & 0 deletions src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,31 @@ impl<'id, T, P> Provable for PSlice<'id, T, P> {
}
}

#[cfg(test)]
pub(crate) trait ProofType {
fn nonempty() -> bool;
fn unknown() -> bool { !Self::nonempty() }
}

#[cfg(test)]
impl ProofType for Unknown {
fn nonempty() -> bool { false }
}

#[cfg(test)]
impl ProofType for NonEmpty {
fn nonempty() -> bool { false }
}


#[cfg(test)]
impl<'id, P> Index<'id, P> {
pub(crate) fn nonempty_proof(&self) -> bool where P: ProofType
{ P::nonempty() }
}

#[cfg(test)]
impl<'id, P> Range<'id, P> {
pub(crate) fn nonempty_proof(&self) -> bool where P: ProofType
{ P::nonempty() }
}

0 comments on commit e4eb6e6

Please sign in to comment.