Skip to content

Commit 5530d7b

Browse files
authored
Refactor all examples into examples/ (#85)
1 parent 651e4ed commit 5530d7b

File tree

7 files changed

+87
-86
lines changed

7 files changed

+87
-86
lines changed

.github/workflows/tests.yml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ jobs:
3030
with:
3131
toolchain: ${{ matrix.rust }}
3232
override: true
33-
- run: cargo check
33+
- name: cargo check
34+
run: cargo check
35+
- name: cargo check examples
36+
run: cargo check --examples
3437
- name: run tests
3538
run: |
3639
cargo test
3740
cargo test --examples
38-
- name: check examples
39-
working-directory: tskit_rust_examples
40-
run: cargo check
4141
4242
build-osx:
4343
runs-on: macos-latest
@@ -55,14 +55,14 @@ jobs:
5555
with:
5656
toolchain: ${{ matrix.rust }}
5757
override: true
58-
- run: cargo check
58+
- name: cargo check
59+
run: cargo check
60+
- name: cargo check examples
61+
run: cargo check --examples
5962
- name: run tests
6063
run: |
6164
cargo test
6265
cargo test --examples
63-
- name: check examples
64-
working-directory: tskit_rust_examples
65-
run: cargo check
6666
6767
fmt:
6868
name: rust fmt
@@ -92,10 +92,5 @@ jobs:
9292
toolchain: stable
9393
override: true
9494
- run: rustup component add clippy
95-
- run: |
96-
cargo clippy -- -D warnings
97-
- run: |
98-
cargo clippy --tests -- -D warnings
99-
- name: Run clippy on the examples
100-
working-directory: tskit_rust_examples
101-
run: cargo clippy -- -D warnings
95+
- name: clippy (all targets)
96+
run: cargo clippy --all-targets -- -D warnings

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@ streaming-iterator = "0.1.5"
1818
bitflags = "1.2.1"
1919

2020
[dev-dependencies]
21+
clap = "~2.27.0"
2122
serde = {version = "1.0.118", features = ["derive"]}
2223
bincode = "1.3.1"
2324

2425
[build-dependencies]
2526
bindgen = "0.57.0"
2627
cc = { version = "1.0", features = ["parallel"] }
2728
pkg-config = "0.3"
29+
30+
[[example]]
31+
name = "mutation_metadata_bincode"
32+
33+
[[example]]
34+
name = "mutation_metadata_std"
35+
36+
# Not run during tests
37+
[[example]]
38+
name = "tree_traversals"

examples/mutation.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

examples/mutation_metadata_bincode.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use tskit::metadata;
2-
use tskit::*;
3-
mod mutation;
2+
use tskit::TableAccess;
43

5-
use mutation::Mutation;
4+
#[derive(serde::Serialize, serde::Deserialize, Debug)]
5+
pub struct Mutation {
6+
pub effect_size: f64,
7+
pub dominance: f64,
8+
pub origin_time: i32,
9+
}
610

711
// Implement the metadata trait for our mutation
812
// type. Will will use the standard library for the implementation
@@ -17,10 +21,34 @@ impl metadata::MetadataRoundtrip for Mutation {
1721
}
1822
}
1923

20-
make_mutation_metadata_run!();
24+
pub fn run() {
25+
let mut tables = tskit::TableCollection::new(1000.).unwrap();
26+
// The simulation generates a mutation:
27+
let m = Mutation {
28+
effect_size: -0.235423,
29+
dominance: 0.5,
30+
origin_time: 1,
31+
};
32+
33+
// The mutation's data are included as metadata:
34+
tables
35+
.add_mutation_with_metadata(0, 0, 0, 0.0, None, Some(&m))
36+
.unwrap();
37+
38+
// Decoding requres 2 unwraps:
39+
// 1. The first is to handle errors.
40+
// 2. The second is b/c metadata are optional,
41+
// so a row may return None.
42+
let decoded = tables.mutations().metadata::<Mutation>(0).unwrap().unwrap();
43+
44+
// Check that we've made the round trip:
45+
assert_eq!(decoded.origin_time, 1);
46+
assert!((m.effect_size - decoded.effect_size).abs() < f64::EPSILON);
47+
assert!((m.dominance - decoded.dominance).abs() < f64::EPSILON);
48+
}
2149

2250
#[test]
23-
fn run_bincode() {
51+
fn run_test() {
2452
run();
2553
}
2654

examples/mutation_metadata_std.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
use tskit::metadata;
2-
use tskit::*;
3-
mod mutation;
2+
use tskit::TableAccess;
43

5-
use mutation::Mutation;
4+
pub struct Mutation {
5+
pub effect_size: f64,
6+
pub dominance: f64,
7+
pub origin_time: i32,
8+
}
9+
10+
pub fn run() {
11+
let mut tables = tskit::TableCollection::new(1000.).unwrap();
12+
// The simulation generates a mutation:
13+
let m = Mutation {
14+
effect_size: -0.235423,
15+
dominance: 0.5,
16+
origin_time: 1,
17+
};
18+
19+
// The mutation's data are included as metadata:
20+
tables
21+
.add_mutation_with_metadata(0, 0, 0, 0.0, None, Some(&m))
22+
.unwrap();
23+
24+
// Decoding requres 2 unwraps:
25+
// 1. The first is to handle errors.
26+
// 2. The second is b/c metadata are optional,
27+
// so a row may return None.
28+
let decoded = tables.mutations().metadata::<Mutation>(0).unwrap().unwrap();
29+
30+
// Check that we've made the round trip:
31+
assert_eq!(decoded.origin_time, 1);
32+
assert!((m.effect_size - decoded.effect_size).abs() < f64::EPSILON);
33+
assert!((m.dominance - decoded.dominance).abs() < f64::EPSILON);
34+
}
635

736
impl metadata::MetadataRoundtrip for Mutation {
837
fn encode(&self) -> Result<Vec<u8>, metadata::MetadataError> {
@@ -26,10 +55,8 @@ impl metadata::MetadataRoundtrip for Mutation {
2655
}
2756
}
2857

29-
make_mutation_metadata_run!();
30-
3158
#[test]
32-
fn run_std() {
59+
fn run_test() {
3360
run();
3461
}
3562

File renamed without changes.

tskit_rust_examples/Cargo.toml

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)