-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1337 from hannobraun/merge
Clean up infrastructure for merging partial objects
- Loading branch information
Showing
11 changed files
with
236 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use iter_fixed::IntoIteratorFixed; | ||
|
||
use crate::storage::Handle; | ||
|
||
/// Trait for merging partial objects | ||
/// | ||
/// Implemented for all partial objects themselves, and also some related types | ||
/// that partial objects usually contain. | ||
pub trait MergeWith: Sized { | ||
/// Merge this object with another | ||
/// | ||
/// # Panics | ||
/// | ||
/// Merging two objects that cannot be merged is considered a programmer | ||
/// error and will result in a panic. | ||
fn merge_with(self, other: impl Into<Self>) -> Self; | ||
} | ||
|
||
/// Wrapper struct that indicates that the contents can be merged | ||
/// | ||
/// Used in connection with [`MergeWith`] to select one implementation over | ||
/// another. | ||
pub struct Mergeable<T>(pub T); | ||
|
||
impl<T, const N: usize> MergeWith for [T; N] | ||
where | ||
T: MergeWith, | ||
{ | ||
fn merge_with(self, other: impl Into<Self>) -> Self { | ||
self.into_iter_fixed() | ||
.zip(other.into()) | ||
.collect::<[_; N]>() | ||
.map(|(a, b)| a.merge_with(b)) | ||
} | ||
} | ||
|
||
impl<T> MergeWith for Option<T> | ||
where | ||
T: PartialEq, | ||
{ | ||
fn merge_with(self, other: impl Into<Self>) -> Self { | ||
let other = other.into(); | ||
|
||
if self == other { | ||
return self; | ||
} | ||
|
||
// We know that `self != other`, or we wouldn't have made it here. | ||
if self.is_some() && other.is_some() { | ||
panic!("Can't merge two `Option`s that are both `Some`") | ||
} | ||
|
||
self.xor(other) | ||
} | ||
} | ||
|
||
// We wouldn't need to use `Mergeable` here, if we had `specialization`: | ||
// https://doc.rust-lang.org/nightly/unstable-book/language-features/specialization.html | ||
// | ||
// Or maybe `min_specialization`: | ||
// https://doc.rust-lang.org/nightly/unstable-book/language-features/min-specialization.html | ||
impl<T> MergeWith for Mergeable<Option<T>> | ||
where | ||
T: MergeWith, | ||
{ | ||
fn merge_with(self, other: impl Into<Self>) -> Self { | ||
let merged = match (self.0, other.into().0) { | ||
(Some(a), Some(b)) => Some(a.merge_with(b)), | ||
(a, b) => a.xor(b), | ||
}; | ||
|
||
Self(merged) | ||
} | ||
} | ||
|
||
impl<T> MergeWith for Vec<T> { | ||
fn merge_with(self, other: impl Into<Self>) -> Self { | ||
let other = other.into(); | ||
|
||
match (self.is_empty(), other.is_empty()) { | ||
(true, true) => { | ||
panic!("Can't merge `PartialHalfEdge`, if both have half-edges") | ||
} | ||
(true, false) => other, | ||
(false, true) => self, | ||
(false, false) => self, // doesn't matter which we use | ||
} | ||
} | ||
} | ||
|
||
impl<T> MergeWith for Mergeable<Vec<T>> { | ||
fn merge_with(mut self, other: impl Into<Self>) -> Self { | ||
self.0.extend(other.into().0); | ||
self | ||
} | ||
} | ||
|
||
impl<T> MergeWith for Handle<T> { | ||
fn merge_with(self, other: impl Into<Self>) -> Self { | ||
if self.id() == other.into().id() { | ||
return self; | ||
} | ||
|
||
panic!("Can't merge two distinct objects") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.