Skip to content

Commit 43f6c34

Browse files
committed
fix arbitrary_from ergonomics by removing the implicit reference in the trait signature
1 parent 1344280 commit 43f6c34

File tree

7 files changed

+57
-59
lines changed

7 files changed

+57
-59
lines changed

simulator/generation/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub trait Arbitrary {
1313
}
1414

1515
pub trait ArbitraryFrom<T> {
16-
fn arbitrary_from<R: Rng>(rng: &mut R, t: &T) -> Self;
16+
fn arbitrary_from<R: Rng>(rng: &mut R, t: T) -> Self;
1717
}
1818

1919
pub(crate) fn frequency<

simulator/generation/plan.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,8 @@ impl InteractionPlan {
283283
}
284284
}
285285

286-
impl InteractionPlan {
287-
// todo: This is a hack to get around the fact that `ArbitraryFrom<T>` can't take a mutable
288-
// reference of T, so instead write a bespoke function without using the trait system.
289-
pub(crate) fn arbitrary_from<R: rand::Rng>(rng: &mut R, env: &mut SimulatorEnv) -> Self {
286+
impl ArbitraryFrom<&mut SimulatorEnv> for InteractionPlan {
287+
fn arbitrary_from<R: rand::Rng>(rng: &mut R, env: &mut SimulatorEnv) -> Self {
290288
let mut plan = InteractionPlan::new();
291289

292290
let num_interactions = env.opts.max_interactions;
@@ -304,7 +302,7 @@ impl InteractionPlan {
304302
plan.plan.len(),
305303
num_interactions
306304
);
307-
let interactions = Interactions::arbitrary_from(rng, &(env, plan.stats()));
305+
let interactions = Interactions::arbitrary_from(rng, (env, plan.stats()));
308306
interactions.shadow(env);
309307

310308
plan.plan.push(interactions);
@@ -471,7 +469,7 @@ fn random_fault<R: rand::Rng>(_rng: &mut R, _env: &SimulatorEnv) -> Interactions
471469
impl ArbitraryFrom<(&SimulatorEnv, InteractionStats)> for Interactions {
472470
fn arbitrary_from<R: rand::Rng>(
473471
rng: &mut R,
474-
(env, stats): &(&SimulatorEnv, InteractionStats),
472+
(env, stats): (&SimulatorEnv, InteractionStats),
475473
) -> Self {
476474
let remaining_read = ((env.opts.max_interactions as f64 * env.opts.read_percent / 100.0)
477475
- (stats.read_count as f64))
@@ -489,7 +487,7 @@ impl ArbitraryFrom<(&SimulatorEnv, InteractionStats)> for Interactions {
489487
(
490488
f64::min(remaining_read, remaining_write) + remaining_create,
491489
Box::new(|rng: &mut R| {
492-
Interactions::Property(Property::arbitrary_from(rng, &(env, stats)))
490+
Interactions::Property(Property::arbitrary_from(rng, (env, &stats)))
493491
}),
494492
),
495493
(

simulator/generation/property.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ impl Property {
115115

116116
interactions
117117
}
118-
Property::DoubleCreateFailure {
119-
create,
120-
queries,
121-
} => {
118+
Property::DoubleCreateFailure { create, queries } => {
122119
let table_name = create.table.name.clone();
123120

124121
let assumption = Interaction::Assumption(Assertion {
@@ -186,7 +183,11 @@ fn remaining(env: &SimulatorEnv, stats: &InteractionStats) -> Remaining {
186183
}
187184
}
188185

189-
fn property_insert_select<R: rand::Rng>(rng: &mut R, env: &SimulatorEnv, remaining: &Remaining) -> Property {
186+
fn property_insert_select<R: rand::Rng>(
187+
rng: &mut R,
188+
env: &SimulatorEnv,
189+
remaining: &Remaining,
190+
) -> Property {
190191
// Get a random table
191192
let table = pick(&env.tables, rng);
192193
// Pick a random column
@@ -218,7 +219,7 @@ fn property_insert_select<R: rand::Rng>(rng: &mut R, env: &SimulatorEnv, remaini
218219
// - [ ] The inserted row will not be updated. (todo: add this constraint once UPDATE is implemented)
219220
// - [ ] The table `t` will not be renamed, dropped, or altered. (todo: add this constraint once ALTER or DROP is implemented)
220221
for _ in 0..rng.gen_range(0..3) {
221-
let query = Query::arbitrary_from(rng, &(table, remaining));
222+
let query = Query::arbitrary_from(rng, (table, remaining));
222223
match &query {
223224
Query::Delete(Delete {
224225
table: t,
@@ -244,7 +245,7 @@ fn property_insert_select<R: rand::Rng>(rng: &mut R, env: &SimulatorEnv, remaini
244245
// Select the row
245246
let select_query = Select {
246247
table: table.name.clone(),
247-
predicate: Predicate::arbitrary_from(rng, &(table, &row)),
248+
predicate: Predicate::arbitrary_from(rng, (table, &row)),
248249
};
249250

250251
Property::InsertSelect {
@@ -254,7 +255,11 @@ fn property_insert_select<R: rand::Rng>(rng: &mut R, env: &SimulatorEnv, remaini
254255
}
255256
}
256257

257-
fn property_double_create_failure<R: rand::Rng>(rng: &mut R, env: &SimulatorEnv, remaining: &Remaining) -> Property {
258+
fn property_double_create_failure<R: rand::Rng>(
259+
rng: &mut R,
260+
env: &SimulatorEnv,
261+
remaining: &Remaining,
262+
) -> Property {
258263
// Get a random table
259264
let table = pick(&env.tables, rng);
260265
// Create the table
@@ -268,7 +273,7 @@ fn property_double_create_failure<R: rand::Rng>(rng: &mut R, env: &SimulatorEnv,
268273
// - [x] There will be no errors in the middle interactions.(best effort)
269274
// - [ ] Table `t` will not be renamed or dropped.(todo: add this constraint once ALTER or DROP is implemented)
270275
for _ in 0..rng.gen_range(0..3) {
271-
let query = Query::arbitrary_from(rng, &(table, remaining));
276+
let query = Query::arbitrary_from(rng, (table, remaining));
272277
match &query {
273278
Query::Create(Create { table: t }) => {
274279
// There will be no errors in the middle interactions.
@@ -288,12 +293,10 @@ fn property_double_create_failure<R: rand::Rng>(rng: &mut R, env: &SimulatorEnv,
288293
}
289294
}
290295

291-
292-
293296
impl ArbitraryFrom<(&SimulatorEnv, &InteractionStats)> for Property {
294297
fn arbitrary_from<R: rand::Rng>(
295298
rng: &mut R,
296-
(env, stats): &(&SimulatorEnv, &InteractionStats),
299+
(env, stats): (&SimulatorEnv, &InteractionStats),
297300
) -> Self {
298301
let remaining_ = remaining(env, stats);
299302
frequency(

simulator/generation/query.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Arbitrary for Create {
1717
}
1818
}
1919

20-
impl ArbitraryFrom<Vec<Table>> for Select {
20+
impl ArbitraryFrom<&Vec<Table>> for Select {
2121
fn arbitrary_from<R: Rng>(rng: &mut R, tables: &Vec<Table>) -> Self {
2222
let table = pick(tables, rng);
2323
Self {
@@ -27,7 +27,7 @@ impl ArbitraryFrom<Vec<Table>> for Select {
2727
}
2828
}
2929

30-
impl ArbitraryFrom<Vec<&Table>> for Select {
30+
impl ArbitraryFrom<&Vec<&Table>> for Select {
3131
fn arbitrary_from<R: Rng>(rng: &mut R, tables: &Vec<&Table>) -> Self {
3232
let table = pick(tables, rng);
3333
Self {
@@ -37,7 +37,7 @@ impl ArbitraryFrom<Vec<&Table>> for Select {
3737
}
3838
}
3939

40-
impl ArbitraryFrom<Table> for Insert {
40+
impl ArbitraryFrom<&Table> for Insert {
4141
fn arbitrary_from<R: Rng>(rng: &mut R, table: &Table) -> Self {
4242
let num_rows = rng.gen_range(1..10);
4343
let values: Vec<Vec<Value>> = (0..num_rows)
@@ -56,7 +56,7 @@ impl ArbitraryFrom<Table> for Insert {
5656
}
5757
}
5858

59-
impl ArbitraryFrom<Table> for Delete {
59+
impl ArbitraryFrom<&Table> for Delete {
6060
fn arbitrary_from<R: Rng>(rng: &mut R, table: &Table) -> Self {
6161
Self {
6262
table: table.name.clone(),
@@ -65,7 +65,7 @@ impl ArbitraryFrom<Table> for Delete {
6565
}
6666
}
6767

68-
impl ArbitraryFrom<Table> for Query {
68+
impl ArbitraryFrom<&Table> for Query {
6969
fn arbitrary_from<R: Rng>(rng: &mut R, table: &Table) -> Self {
7070
frequency(
7171
vec![
@@ -89,7 +89,7 @@ impl ArbitraryFrom<Table> for Query {
8989
}
9090

9191
impl ArbitraryFrom<(&Table, &Remaining)> for Query {
92-
fn arbitrary_from<R: Rng>(rng: &mut R, (table, remaining): &(&Table, &Remaining)) -> Self {
92+
fn arbitrary_from<R: Rng>(rng: &mut R, (table, remaining): (&Table, &Remaining)) -> Self {
9393
frequency(
9494
vec![
9595
(
@@ -98,7 +98,7 @@ impl ArbitraryFrom<(&Table, &Remaining)> for Query {
9898
),
9999
(
100100
remaining.read,
101-
Box::new(|rng| Self::Select(Select::arbitrary_from(rng, &vec![*table]))),
101+
Box::new(|rng| Self::Select(Select::arbitrary_from(rng, &vec![table]))),
102102
),
103103
(
104104
remaining.write,
@@ -118,7 +118,7 @@ struct CompoundPredicate(Predicate);
118118
struct SimplePredicate(Predicate);
119119

120120
impl ArbitraryFrom<(&Table, bool)> for SimplePredicate {
121-
fn arbitrary_from<R: Rng>(rng: &mut R, (table, predicate_value): &(&Table, bool)) -> Self {
121+
fn arbitrary_from<R: Rng>(rng: &mut R, (table, predicate_value): (&Table, bool)) -> Self {
122122
// Pick a random column
123123
let column_index = rng.gen_range(0..table.columns.len());
124124
let column = &table.columns[column_index];
@@ -182,15 +182,15 @@ impl ArbitraryFrom<(&Table, bool)> for SimplePredicate {
182182
}
183183

184184
impl ArbitraryFrom<(&Table, bool)> for CompoundPredicate {
185-
fn arbitrary_from<R: Rng>(rng: &mut R, (table, predicate_value): &(&Table, bool)) -> Self {
185+
fn arbitrary_from<R: Rng>(rng: &mut R, (table, predicate_value): (&Table, bool)) -> Self {
186186
// Decide if you want to create an AND or an OR
187187
Self(if rng.gen_bool(0.7) {
188188
// An AND for true requires each of its children to be true
189189
// An AND for false requires at least one of its children to be false
190-
if *predicate_value {
190+
if predicate_value {
191191
Predicate::And(
192192
(0..rng.gen_range(0..=3))
193-
.map(|_| SimplePredicate::arbitrary_from(rng, &(*table, true)).0)
193+
.map(|_| SimplePredicate::arbitrary_from(rng, (table, true)).0)
194194
.collect(),
195195
)
196196
} else {
@@ -209,14 +209,14 @@ impl ArbitraryFrom<(&Table, bool)> for CompoundPredicate {
209209
Predicate::And(
210210
booleans
211211
.iter()
212-
.map(|b| SimplePredicate::arbitrary_from(rng, &(*table, *b)).0)
212+
.map(|b| SimplePredicate::arbitrary_from(rng, (table, *b)).0)
213213
.collect(),
214214
)
215215
}
216216
} else {
217217
// An OR for true requires at least one of its children to be true
218218
// An OR for false requires each of its children to be false
219-
if *predicate_value {
219+
if predicate_value {
220220
// Create a vector of random booleans
221221
let mut booleans = (0..rng.gen_range(0..=3))
222222
.map(|_| rng.gen_bool(0.5))
@@ -230,42 +230,42 @@ impl ArbitraryFrom<(&Table, bool)> for CompoundPredicate {
230230
Predicate::Or(
231231
booleans
232232
.iter()
233-
.map(|b| SimplePredicate::arbitrary_from(rng, &(*table, *b)).0)
233+
.map(|b| SimplePredicate::arbitrary_from(rng, (table, *b)).0)
234234
.collect(),
235235
)
236236
} else {
237237
Predicate::Or(
238238
(0..rng.gen_range(0..=3))
239-
.map(|_| SimplePredicate::arbitrary_from(rng, &(*table, false)).0)
239+
.map(|_| SimplePredicate::arbitrary_from(rng, (table, false)).0)
240240
.collect(),
241241
)
242242
}
243243
})
244244
}
245245
}
246246

247-
impl ArbitraryFrom<Table> for Predicate {
247+
impl ArbitraryFrom<&Table> for Predicate {
248248
fn arbitrary_from<R: Rng>(rng: &mut R, table: &Table) -> Self {
249249
let predicate_value = rng.gen_bool(0.5);
250-
CompoundPredicate::arbitrary_from(rng, &(table, predicate_value)).0
250+
CompoundPredicate::arbitrary_from(rng, (table, predicate_value)).0
251251
}
252252
}
253253

254254
impl ArbitraryFrom<(&str, &Value)> for Predicate {
255-
fn arbitrary_from<R: Rng>(rng: &mut R, (column_name, value): &(&str, &Value)) -> Self {
255+
fn arbitrary_from<R: Rng>(rng: &mut R, (column_name, value): (&str, &Value)) -> Self {
256256
one_of(
257257
vec![
258258
Box::new(|_| Predicate::Eq(column_name.to_string(), (*value).clone())),
259259
Box::new(|rng| {
260260
Self::Gt(
261261
column_name.to_string(),
262-
GTValue::arbitrary_from(rng, *value).0,
262+
GTValue::arbitrary_from(rng, value).0,
263263
)
264264
}),
265265
Box::new(|rng| {
266266
Self::Lt(
267267
column_name.to_string(),
268-
LTValue::arbitrary_from(rng, *value).0,
268+
LTValue::arbitrary_from(rng, value).0,
269269
)
270270
}),
271271
],
@@ -275,7 +275,7 @@ impl ArbitraryFrom<(&str, &Value)> for Predicate {
275275
}
276276

277277
/// Produces a predicate that is true for the provided row in the given table
278-
fn produce_true_predicate<R: Rng>(rng: &mut R, (t, row): &(&Table, &Vec<Value>)) -> Predicate {
278+
fn produce_true_predicate<R: Rng>(rng: &mut R, (t, row): (&Table, &Vec<Value>)) -> Predicate {
279279
// Pick a column
280280
let column_index = rng.gen_range(0..t.columns.len());
281281
let column = &t.columns[column_index];
@@ -304,7 +304,7 @@ fn produce_true_predicate<R: Rng>(rng: &mut R, (t, row): &(&Table, &Vec<Value>))
304304
}
305305

306306
/// Produces a predicate that is false for the provided row in the given table
307-
fn produce_false_predicate<R: Rng>(rng: &mut R, (t, row): &(&Table, &Vec<Value>)) -> Predicate {
307+
fn produce_false_predicate<R: Rng>(rng: &mut R, (t, row): (&Table, &Vec<Value>)) -> Predicate {
308308
// Pick a column
309309
let column_index = rng.gen_range(0..t.columns.len());
310310
let column = &t.columns[column_index];
@@ -333,18 +333,18 @@ fn produce_false_predicate<R: Rng>(rng: &mut R, (t, row): &(&Table, &Vec<Value>)
333333
}
334334

335335
impl ArbitraryFrom<(&Table, &Vec<Value>)> for Predicate {
336-
fn arbitrary_from<R: Rng>(rng: &mut R, (t, row): &(&Table, &Vec<Value>)) -> Self {
336+
fn arbitrary_from<R: Rng>(rng: &mut R, (t, row): (&Table, &Vec<Value>)) -> Self {
337337
// We want to produce a predicate that is true for the row
338338
// We can do this by creating several predicates that
339339
// are true, some that are false, combiend them in ways that correspond to the creation of a true predicate
340340

341341
// Produce some true and false predicates
342342
let mut true_predicates = (1..=rng.gen_range(1..=4))
343-
.map(|_| produce_true_predicate(rng, &(*t, row)))
343+
.map(|_| produce_true_predicate(rng, (t, row)))
344344
.collect::<Vec<_>>();
345345

346346
let false_predicates = (0..=rng.gen_range(0..=3))
347-
.map(|_| produce_false_predicate(rng, &(*t, row)))
347+
.map(|_| produce_false_predicate(rng, (t, row)))
348348
.collect::<Vec<_>>();
349349

350350
// Start building a top level predicate from a true predicate

simulator/generation/table.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Arbitrary for ColumnType {
4545
}
4646
}
4747

48-
impl ArbitraryFrom<Vec<&Value>> for Value {
48+
impl ArbitraryFrom<&Vec<&Value>> for Value {
4949
fn arbitrary_from<R: Rng>(rng: &mut R, values: &Vec<&Self>) -> Self {
5050
if values.is_empty() {
5151
return Self::Null;
@@ -55,7 +55,7 @@ impl ArbitraryFrom<Vec<&Value>> for Value {
5555
}
5656
}
5757

58-
impl ArbitraryFrom<ColumnType> for Value {
58+
impl ArbitraryFrom<&ColumnType> for Value {
5959
fn arbitrary_from<R: Rng>(rng: &mut R, column_type: &ColumnType) -> Self {
6060
match column_type {
6161
ColumnType::Integer => Self::Integer(rng.gen_range(i64::MIN..i64::MAX)),
@@ -68,7 +68,7 @@ impl ArbitraryFrom<ColumnType> for Value {
6868

6969
pub(crate) struct LTValue(pub(crate) Value);
7070

71-
impl ArbitraryFrom<Vec<&Value>> for LTValue {
71+
impl ArbitraryFrom<&Vec<&Value>> for LTValue {
7272
fn arbitrary_from<R: Rng>(rng: &mut R, values: &Vec<&Value>) -> Self {
7373
if values.is_empty() {
7474
return Self(Value::Null);
@@ -79,7 +79,7 @@ impl ArbitraryFrom<Vec<&Value>> for LTValue {
7979
}
8080
}
8181

82-
impl ArbitraryFrom<Value> for LTValue {
82+
impl ArbitraryFrom<&Value> for LTValue {
8383
fn arbitrary_from<R: Rng>(rng: &mut R, value: &Value) -> Self {
8484
match value {
8585
Value::Integer(i) => Self(Value::Integer(rng.gen_range(i64::MIN..*i - 1))),
@@ -128,7 +128,7 @@ impl ArbitraryFrom<Value> for LTValue {
128128

129129
pub(crate) struct GTValue(pub(crate) Value);
130130

131-
impl ArbitraryFrom<Vec<&Value>> for GTValue {
131+
impl ArbitraryFrom<&Vec<&Value>> for GTValue {
132132
fn arbitrary_from<R: Rng>(rng: &mut R, values: &Vec<&Value>) -> Self {
133133
if values.is_empty() {
134134
return Self(Value::Null);
@@ -139,7 +139,7 @@ impl ArbitraryFrom<Vec<&Value>> for GTValue {
139139
}
140140
}
141141

142-
impl ArbitraryFrom<Value> for GTValue {
142+
impl ArbitraryFrom<&Value> for GTValue {
143143
fn arbitrary_from<R: Rng>(rng: &mut R, value: &Value) -> Self {
144144
match value {
145145
Value::Integer(i) => Self(Value::Integer(rng.gen_range(*i..i64::MAX))),

simulator/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use clap::Parser;
33
use core::panic;
44
use generation::plan::{InteractionPlan, InteractionPlanState};
5+
use generation::ArbitraryFrom;
56
use limbo_core::Database;
67
use rand::prelude::*;
78
use rand_chacha::ChaCha8Rng;

0 commit comments

Comments
 (0)