Skip to content

Commit f94c3b2

Browse files
authored
Merge pull request #11 from eddyb/none-of-you-are-free-of-default
Implement `Default` on `SnapshotVec`, `UnificationTable` and unification stores.
2 parents 2d9d6b2 + 8dc607d commit f94c3b2

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description = "Union-find, congruence closure, and other unification code. Based
44
license = "MIT/Apache-2.0"
55
homepage = "https://github.com/nikomatsakis/ena"
66
repository = "https://github.com/nikomatsakis/ena"
7-
version = "0.9.3"
7+
version = "0.10.0"
88
authors = ["Niko Matsakis <[email protected]>"]
99
readme = "README.md"
1010
keywords = ["unification", "union-find"]

src/snapshot_vec.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,20 @@ pub trait SnapshotVecDelegate {
7575
fn reverse(values: &mut Vec<Self::Value>, action: Self::Undo);
7676
}
7777

78-
impl<D: SnapshotVecDelegate> SnapshotVec<D> {
79-
pub fn new() -> SnapshotVec<D> {
78+
// HACK(eddyb) manual impl avoids `Default` bound on `D`.
79+
impl<D: SnapshotVecDelegate> Default for SnapshotVec<D> {
80+
fn default() -> Self {
8081
SnapshotVec {
8182
values: Vec::new(),
8283
undo_log: Vec::new(),
8384
}
8485
}
86+
}
87+
88+
impl<D: SnapshotVecDelegate> SnapshotVec<D> {
89+
pub fn new() -> Self {
90+
Self::default()
91+
}
8592

8693
pub fn with_capacity(c: usize) -> SnapshotVec<D> {
8794
SnapshotVec {

src/unify/backing_vec.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ type Key<S> = <S as UnificationStore>::Key;
1212
/// Largely internal trait implemented by the unification table
1313
/// backing store types. The most common such type is `InPlace`,
1414
/// which indicates a standard, mutable unification table.
15-
pub trait UnificationStore: ops::Index<usize, Output = VarValue<Key<Self>>> + Clone {
15+
pub trait UnificationStore:
16+
ops::Index<usize, Output = VarValue<Key<Self>>> + Clone + Default
17+
{
1618
type Key: UnifyKey<Value = Self::Value>;
1719
type Value: UnifyValue;
1820
type Snapshot;
1921

20-
fn new() -> Self;
21-
2222
fn start_snapshot(&mut self) -> Self::Snapshot;
2323

2424
fn rollback_to(&mut self, snapshot: Self::Snapshot);
@@ -51,16 +51,18 @@ pub struct InPlace<K: UnifyKey> {
5151
values: sv::SnapshotVec<Delegate<K>>
5252
}
5353

54+
// HACK(eddyb) manual impl avoids `Default` bound on `K`.
55+
impl<K: UnifyKey> Default for InPlace<K> {
56+
fn default() -> Self {
57+
InPlace { values: sv::SnapshotVec::new() }
58+
}
59+
}
60+
5461
impl<K: UnifyKey> UnificationStore for InPlace<K> {
5562
type Key = K;
5663
type Value = K::Value;
5764
type Snapshot = sv::Snapshot;
5865

59-
#[inline]
60-
fn new() -> Self {
61-
InPlace { values: sv::SnapshotVec::new() }
62-
}
63-
6466
#[inline]
6567
fn start_snapshot(&mut self) -> Self::Snapshot {
6668
self.values.start_snapshot()
@@ -132,17 +134,20 @@ pub struct Persistent<K: UnifyKey> {
132134
values: DVec<VarValue<K>>
133135
}
134136

137+
// HACK(eddyb) manual impl avoids `Default` bound on `K`.
138+
#[cfg(feature = "persistent")]
139+
impl<K: UnifyKey> Default for Persistent<K> {
140+
fn default() -> Self {
141+
Persistent { values: DVec::new() }
142+
}
143+
}
144+
135145
#[cfg(feature = "persistent")]
136146
impl<K: UnifyKey> UnificationStore for Persistent<K> {
137147
type Key = K;
138148
type Value = K::Value;
139149
type Snapshot = Self;
140150

141-
#[inline]
142-
fn new() -> Self {
143-
Persistent { values: DVec::new() }
144-
}
145-
146151
#[inline]
147152
fn start_snapshot(&mut self) -> Self::Snapshot {
148153
self.clone()

src/unify/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub struct VarValue<K: UnifyKey> { // FIXME pub
174174
/// cloning the table is an O(1) operation.
175175
/// - This implies that ordinary operations are quite a bit slower though.
176176
/// - Requires the `persistent` feature be selected in your Cargo.toml file.
177-
#[derive(Clone, Debug)]
177+
#[derive(Clone, Debug, Default)]
178178
pub struct UnificationTable<S: UnificationStore> {
179179
/// Indicates the current value of each key.
180180
values: S,
@@ -237,9 +237,7 @@ impl<K: UnifyKey> VarValue<K> {
237237

238238
impl<S: UnificationStore> UnificationTable<S> {
239239
pub fn new() -> Self {
240-
UnificationTable {
241-
values: S::new()
242-
}
240+
Self::default()
243241
}
244242

245243
/// Starts a new snapshot. Each snapshot must be either

0 commit comments

Comments
 (0)