From aa130c9d3a21df6ba229906c88ad9daeefa08b27 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 08:44:10 -0700 Subject: [PATCH 01/29] new table access trait --- src/traits.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/traits.rs b/src/traits.rs index fbc03532d..c17c58300 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -119,3 +119,55 @@ impl_individual_parents!( ); impl_individual_parents!(N, usize, &[crate::IndividualId; N], self, self.as_slice()); impl_individual_parents!(N, usize, [crate::IndividualId; N], self, self.as_slice()); + +pub trait TableAccess { + fn edges(&self) -> &crate::EdgeTable; + fn nodes(&self) -> &crate::NodeTable; + fn sites(&self) -> &crate::SiteTable; + fn mutations(&self) -> &crate::MutationTable; + fn migrations(&self) -> &crate::MigrationTable; +} + +impl TableAccess for crate::TableCollection { + fn edges(&self) -> &crate::EdgeTable { + self.edges() + } + + fn nodes(&self) -> &crate::NodeTable { + self.nodes() + } + + fn sites(&self) -> &crate::SiteTable { + self.sites() + } + + fn mutations(&self) -> &crate::MutationTable { + self.mutations() + } + + fn migrations(&self) -> &crate::MigrationTable { + self.migrations() + } +} + +impl TableAccess for crate::TreeSequence { + fn edges(&self) -> &crate::EdgeTable { + self.tables().edges() + } + + fn nodes(&self) -> &crate::NodeTable { + self.tables().nodes() + } + + fn sites(&self) -> &crate::SiteTable { + self.tables().sites() + } + + fn mutations(&self) -> &crate::MutationTable { + self.tables().mutations() + } + + fn migrations(&self) -> &crate::MigrationTable { + self.tables().migrations() + } +} From cf6477824645af09810a28b3104a1166848d3868 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 09:09:57 -0700 Subject: [PATCH 02/29] make trait pub, add to prelude --- src/lib.rs | 1 + src/prelude.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index d91935c73..a729a94c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,6 +113,7 @@ pub use site_table::{SiteTable, SiteTableRow}; pub use sys::flags::*; pub use table_collection::TableCollection; pub use traits::IndividualLocation; +pub use traits::TableAccess; pub use traits::IndividualParents; pub use tree_interface::{NodeTraversalOrder, TreeInterface}; pub use trees::{Tree, TreeSequence}; diff --git a/src/prelude.rs b/src/prelude.rs index 9392aa335..b548abc71 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -7,3 +7,4 @@ pub use { crate::NodeId, crate::PopulationId, crate::Position, crate::RawFlags, crate::SiteId, crate::SizeType, crate::Time, }; +pub use crate::TableAccess; From b8ca8ddc053029b12ec04f064b2e9a4213f421df Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 09:15:15 -0700 Subject: [PATCH 03/29] new trait --- src/traits.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/traits.rs b/src/traits.rs index c17c58300..70cc26c64 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -128,6 +128,12 @@ pub trait TableAccess { fn migrations(&self) -> &crate::MigrationTable; } +pub trait TableIteration: TableAccess { + fn edges_iter(&self) -> impl Iterator + '_ { + self.edges().iter() + } +} + impl TableAccess for crate::TableCollection { fn edges(&self) -> &crate::EdgeTable { self.edges() @@ -171,3 +177,5 @@ impl TableAccess for crate::TreeSequence { self.tables().migrations() } } + +impl TableIteration for T where T: TableAccess {} From db5d013367065f2b08843903db94a4c03d5973a3 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 09:28:24 -0700 Subject: [PATCH 04/29] add basic test --- src/lib.rs | 3 ++- src/prelude.rs | 1 + tests/test_table_traits.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/test_table_traits.rs diff --git a/src/lib.rs b/src/lib.rs index a729a94c4..81b3c82b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,8 +113,9 @@ pub use site_table::{SiteTable, SiteTableRow}; pub use sys::flags::*; pub use table_collection::TableCollection; pub use traits::IndividualLocation; -pub use traits::TableAccess; pub use traits::IndividualParents; +pub use traits::TableAccess; +pub use traits::TableIteration; pub use tree_interface::{NodeTraversalOrder, TreeInterface}; pub use trees::{Tree, TreeSequence}; diff --git a/src/prelude.rs b/src/prelude.rs index b548abc71..0be7db5ff 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -8,3 +8,4 @@ pub use { crate::SizeType, crate::Time, }; pub use crate::TableAccess; +pub use crate::TableIteration; diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs new file mode 100644 index 000000000..65939a55e --- /dev/null +++ b/tests/test_table_traits.rs @@ -0,0 +1,34 @@ +fn make_tables() -> tskit::TableCollection { + let mut tables = tskit::TableCollection::new(100.).unwrap(); + tables + .add_node(tskit::NodeFlags::default(), 0.0, -1, -1) + .unwrap(); + tables + .add_node(tskit::NodeFlags::default(), 1.0, -1, -1) + .unwrap(); + tables + .add_node(tskit::NodeFlags::default(), 1.0, -1, -1) + .unwrap(); + tables.add_edge(0., 50., 1, 0).unwrap(); + tables.add_edge(0., 50., 2, 0).unwrap(); + tables +} + +fn get_edges_from_tables(tables: &tskit::TableCollection) -> Vec { + tables.edges().iter().collect::>() +} + +fn get_edges_via_table_iteration_trait(tables: &T) -> Vec +where + T: tskit::TableIteration, +{ + tables.edges().iter().collect::>() +} + +#[test] +fn test_table_collection_edge_iteration() { + let tables = make_tables(); + let v0 = get_edges_from_tables(&tables); + let v1 = get_edges_via_table_iteration_trait(&tables); + assert_eq!(v0, v1); +} From 79741ba996acfa0bc7b9b10e06d57d80d8483d25 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 09:43:48 -0700 Subject: [PATCH 05/29] trait not object safe --- tests/test_table_traits.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 65939a55e..2f93cc1a0 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -25,6 +25,11 @@ where tables.edges().iter().collect::>() } +fn get_edges_via_table_iteration_trait_object(tables: &dyn tskit::TableIteration) -> Vec +{ + tables.edges().iter().collect::>() +} + #[test] fn test_table_collection_edge_iteration() { let tables = make_tables(); @@ -32,3 +37,10 @@ fn test_table_collection_edge_iteration() { let v1 = get_edges_via_table_iteration_trait(&tables); assert_eq!(v0, v1); } +#[test] +fn test_table_collection_edge_iteration_object_safety() { + let tables = Box::new(make_tables()); + let v0 = get_edges_from_tables(&tables); + let v1 = get_edges_via_table_iteration_trait_object(&tables); + assert_eq!(v0, v1); +} From 2c28bcd20053894be4f84e5e81cde1dab84f7a46 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 10:36:44 -0700 Subject: [PATCH 06/29] object safety, at the cost of an indirection --- src/traits.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index 70cc26c64..47b95f57a 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -129,8 +129,8 @@ pub trait TableAccess { } pub trait TableIteration: TableAccess { - fn edges_iter(&self) -> impl Iterator + '_ { - self.edges().iter() + fn edges_iter(&self) -> Box + '_> { + Box::new(self.edges().iter()) } } @@ -178,4 +178,52 @@ impl TableAccess for crate::TreeSequence { } } +impl TableAccess for &T +where + T: TableAccess, +{ + fn migrations(&self) -> &crate::MigrationTable { + T::migrations(&self) + } + + fn mutations(&self) -> &crate::MutationTable { + T::mutations(self) + } + + fn edges(&self) -> &crate::EdgeTable { + T::edges(self) + } + fn sites(&self) -> &crate::SiteTable { + T::sites(self) + } + fn nodes(&self) -> &crate::NodeTable { + T::nodes(self) + } +} + +impl TableAccess for Box +where + T: TableAccess, +{ + fn migrations(&self) -> &crate::MigrationTable { + self.as_ref().migrations() + } + + fn edges(&self) -> &crate::EdgeTable { + self.as_ref().edges() + } + + fn nodes(&self) -> &crate::NodeTable { + todo!() + } + + fn sites(&self) -> &crate::SiteTable { + todo!() + } + + fn mutations(&self) -> &crate::MutationTable { + todo!() + } +} + impl TableIteration for T where T: TableAccess {} From 43ebd2de75d145831e9b07aad276fddfef3c200c Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 10:37:15 -0700 Subject: [PATCH 07/29] lint --- src/prelude.rs | 4 ++-- src/traits.rs | 2 +- tests/test_table_traits.rs | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/prelude.rs b/src/prelude.rs index 0be7db5ff..f4d6ec63e 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,5 +1,7 @@ //! Export commonly-use types and traits +pub use crate::TableAccess; +pub use crate::TableIteration; pub use streaming_iterator::DoubleEndedStreamingIterator; pub use streaming_iterator::StreamingIterator; pub use { @@ -7,5 +9,3 @@ pub use { crate::NodeId, crate::PopulationId, crate::Position, crate::RawFlags, crate::SiteId, crate::SizeType, crate::Time, }; -pub use crate::TableAccess; -pub use crate::TableIteration; diff --git a/src/traits.rs b/src/traits.rs index 47b95f57a..24965518a 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -183,7 +183,7 @@ where T: TableAccess, { fn migrations(&self) -> &crate::MigrationTable { - T::migrations(&self) + T::migrations(self) } fn mutations(&self) -> &crate::MutationTable { diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 2f93cc1a0..073f161ed 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -25,8 +25,9 @@ where tables.edges().iter().collect::>() } -fn get_edges_via_table_iteration_trait_object(tables: &dyn tskit::TableIteration) -> Vec -{ +fn get_edges_via_table_iteration_trait_object( + tables: &dyn tskit::TableIteration, +) -> Vec { tables.edges().iter().collect::>() } From b059e065e6e92007e12e912ad578b17adf660f4f Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 14:54:43 -0700 Subject: [PATCH 08/29] object safety, flexibility, but what about ergonomics? --- src/lib.rs | 1 + src/traits.rs | 7 +++++++ tests/test_table_traits.rs | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 81b3c82b9..f25c00947 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,7 @@ pub use traits::IndividualLocation; pub use traits::IndividualParents; pub use traits::TableAccess; pub use traits::TableIteration; +pub use traits::ObjectSafeTableIteration; pub use tree_interface::{NodeTraversalOrder, TreeInterface}; pub use trees::{Tree, TreeSequence}; diff --git a/src/traits.rs b/src/traits.rs index 24965518a..b871ed9d5 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -129,6 +129,12 @@ pub trait TableAccess { } pub trait TableIteration: TableAccess { + fn edges_iter(&self) -> impl Iterator + '_ { + self.edges().iter() + } +} + +pub trait ObjectSafeTableIteration: TableAccess { fn edges_iter(&self) -> Box + '_> { Box::new(self.edges().iter()) } @@ -227,3 +233,4 @@ where } impl TableIteration for T where T: TableAccess {} +impl ObjectSafeTableIteration for T where T: TableAccess {} diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 073f161ed..3956c72c0 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -26,9 +26,9 @@ where } fn get_edges_via_table_iteration_trait_object( - tables: &dyn tskit::TableIteration, + tables: &dyn tskit::ObjectSafeTableIteration, ) -> Vec { - tables.edges().iter().collect::>() + tables.edges_iter().collect::>() } #[test] From cf9ef54393525621eea487b665d1316229ac728f Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 14:54:57 -0700 Subject: [PATCH 09/29] fmt --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f25c00947..de4ba745b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,9 +114,9 @@ pub use sys::flags::*; pub use table_collection::TableCollection; pub use traits::IndividualLocation; pub use traits::IndividualParents; +pub use traits::ObjectSafeTableIteration; pub use traits::TableAccess; pub use traits::TableIteration; -pub use traits::ObjectSafeTableIteration; pub use tree_interface::{NodeTraversalOrder, TreeInterface}; pub use trees::{Tree, TreeSequence}; From 8d03a6c6b2f0258f8d68b0990eb65592faeaf8fb Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 15:31:50 -0700 Subject: [PATCH 10/29] some fixes --- src/traits.rs | 4 ++-- tests/test_table_traits.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index b871ed9d5..45921ef7c 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -232,5 +232,5 @@ where } } -impl TableIteration for T where T: TableAccess {} -impl ObjectSafeTableIteration for T where T: TableAccess {} +impl TableIteration for T where T: TableAccess + ?Sized {} +impl ObjectSafeTableIteration for T where T: TableAccess + ?Sized {} diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 3956c72c0..3bf198c0d 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -28,7 +28,7 @@ where fn get_edges_via_table_iteration_trait_object( tables: &dyn tskit::ObjectSafeTableIteration, ) -> Vec { - tables.edges_iter().collect::>() + tskit::ObjectSafeTableIteration::edges_iter(tables).collect::>() } #[test] From 63a79626f7d7f5fbd1af3e210c30d6cce206a1a7 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 15:39:45 -0700 Subject: [PATCH 11/29] missed a table --- src/traits.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/traits.rs b/src/traits.rs index 45921ef7c..90d5e376a 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -126,6 +126,7 @@ pub trait TableAccess { fn sites(&self) -> &crate::SiteTable; fn mutations(&self) -> &crate::MutationTable; fn migrations(&self) -> &crate::MigrationTable; + fn populations(&self) -> &crate::PopulationTable; } pub trait TableIteration: TableAccess { @@ -160,6 +161,10 @@ impl TableAccess for crate::TableCollection { fn migrations(&self) -> &crate::MigrationTable { self.migrations() } + + fn populations(&self) -> &crate::PopulationTable { + self.populations() + } } impl TableAccess for crate::TreeSequence { @@ -182,6 +187,10 @@ impl TableAccess for crate::TreeSequence { fn migrations(&self) -> &crate::MigrationTable { self.tables().migrations() } + + fn populations(&self) -> &crate::PopulationTable { + self.tables().populations() + } } impl TableAccess for &T @@ -205,6 +214,9 @@ where fn nodes(&self) -> &crate::NodeTable { T::nodes(self) } + fn populations(&self) -> &crate::PopulationTable { + T::populations(&self) + } } impl TableAccess for Box @@ -230,6 +242,10 @@ where fn mutations(&self) -> &crate::MutationTable { todo!() } + + fn populations(&self) -> &crate::PopulationTable { + todo!() + } } impl TableIteration for T where T: TableAccess + ?Sized {} From 63eacd8b52bb4bdaf0054b1c53c705ebad8c783b Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 11 Jul 2024 15:46:07 -0700 Subject: [PATCH 12/29] clippy gotta clip --- src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/traits.rs b/src/traits.rs index 90d5e376a..1718d08b9 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -215,7 +215,7 @@ where T::nodes(self) } fn populations(&self) -> &crate::PopulationTable { - T::populations(&self) + T::populations(self) } } From 0993b22e12f288efcf1121e40fdd97d5a2964d68 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 09:08:50 -0700 Subject: [PATCH 13/29] up msrv to 1.75.0 --- .github/workflows/tests.yml | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 36d781636..f913aa286 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -83,7 +83,7 @@ jobs: strategy: matrix: rust: - - 1.71.0 + - 1.75.0 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@v1 diff --git a/Cargo.toml b/Cargo.toml index d919f0c9b..8886dae02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ description = "rust interface to tskit" license = "MIT" homepage = "https://github.com/tskit-dev/tskit-rust" repository = "https://github.com/tskit-dev/tskit-rust" -rust-version = "1.71.0" +rust-version = "1.75.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lints.rust] From f64dd4286ff8bd5ff0a58af04c622c0ab429d9b0 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 09:19:23 -0700 Subject: [PATCH 14/29] add tests. starting to repeat ourselves --- src/traits.rs | 3 +++ tests/test_table_traits.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/traits.rs b/src/traits.rs index 1718d08b9..d5efa3fe2 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -139,6 +139,9 @@ pub trait ObjectSafeTableIteration: TableAccess { fn edges_iter(&self) -> Box + '_> { Box::new(self.edges().iter()) } + fn populations_iter(&self) -> Box + '_> { + Box::new(self.populations().iter()) + } } impl TableAccess for crate::TableCollection { diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 3bf198c0d..82fe2aa89 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -18,6 +18,10 @@ fn get_edges_from_tables(tables: &tskit::TableCollection) -> Vec>() } +fn get_populations_from_tables(tables: &tskit::TableCollection) -> Vec { + tables.populations().iter().collect::>() +} + fn get_edges_via_table_iteration_trait(tables: &T) -> Vec where T: tskit::TableIteration, @@ -25,12 +29,25 @@ where tables.edges().iter().collect::>() } +fn get_populations_via_table_iteration_trait(tables: &T) -> Vec +where + T: tskit::TableIteration, +{ + tables.populations().iter().collect::>() +} + fn get_edges_via_table_iteration_trait_object( tables: &dyn tskit::ObjectSafeTableIteration, ) -> Vec { tskit::ObjectSafeTableIteration::edges_iter(tables).collect::>() } +fn get_populations_via_table_iteration_trait_object( + tables: &dyn tskit::ObjectSafeTableIteration, +) -> Vec { + tskit::ObjectSafeTableIteration::populations_iter(tables).collect::>() +} + #[test] fn test_table_collection_edge_iteration() { let tables = make_tables(); @@ -38,6 +55,15 @@ fn test_table_collection_edge_iteration() { let v1 = get_edges_via_table_iteration_trait(&tables); assert_eq!(v0, v1); } + +#[test] +fn test_table_collection_population_iteration() { + let tables = make_tables(); + let v0 = get_populations_from_tables(&tables); + let v1 = get_populations_via_table_iteration_trait(&tables); + assert_eq!(v0, v1); +} + #[test] fn test_table_collection_edge_iteration_object_safety() { let tables = Box::new(make_tables()); @@ -45,3 +71,11 @@ fn test_table_collection_edge_iteration_object_safety() { let v1 = get_edges_via_table_iteration_trait_object(&tables); assert_eq!(v0, v1); } + +#[test] +fn test_table_collection_population_iteration_object_safety() { + let tables = Box::new(make_tables()); + let v0 = get_populations_from_tables(&tables); + let v1 = get_populations_via_table_iteration_trait_object(&tables); + assert_eq!(v0, v1); +} From 5b77844e9316414ca6fd6d1ec8c2e4c8a60253a1 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 09:44:11 -0700 Subject: [PATCH 15/29] DRY in the tests --- src/traits.rs | 2 +- tests/test_table_traits.rs | 113 +++++++++++++++++-------------------- 2 files changed, 52 insertions(+), 63 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index d5efa3fe2..619afe121 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -235,7 +235,7 @@ where } fn nodes(&self) -> &crate::NodeTable { - todo!() + self.as_ref().nodes() } fn sites(&self) -> &crate::SiteTable { diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 82fe2aa89..87636aa8b 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -1,3 +1,52 @@ +#[derive(PartialEq, Debug)] +struct IteratorOutput { + edges: Vec, + nodes: Vec, +} + +impl IteratorOutput { + fn new_from_tables(tables: &tskit::TableCollection) -> Self { + let edges = tables.edges().iter().collect::>(); + let nodes = tables.nodes().iter().collect::>(); + Self { edges, nodes } + } + + fn new_from_table_access(access: &T) -> Self + where + T: tskit::TableAccess, + { + let edges = access.edges().iter().collect::>(); + let nodes = access.nodes().iter().collect::>(); + Self { edges, nodes } + } + + fn new_from_table_iteration(iterator: &T) -> Self + where + T: tskit::TableIteration, + { + let edges = iterator.edges().iter().collect::>(); + let nodes = iterator.nodes().iter().collect::>(); + Self { edges, nodes } + } + + fn new_from_dyn(dynamic: &dyn tskit::ObjectSafeTableIteration) -> Self { + let edges = dynamic.edges().iter().collect::>(); + let nodes = dynamic.nodes().iter().collect::>(); + Self { edges, nodes } + } +} + +fn validate_output_from_tables(tables: tskit::TableCollection) { + let tables_output = IteratorOutput::new_from_tables(&tables); + let access_output = IteratorOutput::new_from_table_access(&tables); + assert_eq!(tables_output, access_output); + let iteration_output = IteratorOutput::new_from_table_iteration(&tables); + assert_eq!(tables_output, iteration_output); + let boxed = Box::new(tables); + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(tables_output, dynamic_output); +} + fn make_tables() -> tskit::TableCollection { let mut tables = tskit::TableCollection::new(100.).unwrap(); tables @@ -14,68 +63,8 @@ fn make_tables() -> tskit::TableCollection { tables } -fn get_edges_from_tables(tables: &tskit::TableCollection) -> Vec { - tables.edges().iter().collect::>() -} - -fn get_populations_from_tables(tables: &tskit::TableCollection) -> Vec { - tables.populations().iter().collect::>() -} - -fn get_edges_via_table_iteration_trait(tables: &T) -> Vec -where - T: tskit::TableIteration, -{ - tables.edges().iter().collect::>() -} - -fn get_populations_via_table_iteration_trait(tables: &T) -> Vec -where - T: tskit::TableIteration, -{ - tables.populations().iter().collect::>() -} - -fn get_edges_via_table_iteration_trait_object( - tables: &dyn tskit::ObjectSafeTableIteration, -) -> Vec { - tskit::ObjectSafeTableIteration::edges_iter(tables).collect::>() -} - -fn get_populations_via_table_iteration_trait_object( - tables: &dyn tskit::ObjectSafeTableIteration, -) -> Vec { - tskit::ObjectSafeTableIteration::populations_iter(tables).collect::>() -} - #[test] -fn test_table_collection_edge_iteration() { +fn test_traits_with_table_collection() { let tables = make_tables(); - let v0 = get_edges_from_tables(&tables); - let v1 = get_edges_via_table_iteration_trait(&tables); - assert_eq!(v0, v1); -} - -#[test] -fn test_table_collection_population_iteration() { - let tables = make_tables(); - let v0 = get_populations_from_tables(&tables); - let v1 = get_populations_via_table_iteration_trait(&tables); - assert_eq!(v0, v1); -} - -#[test] -fn test_table_collection_edge_iteration_object_safety() { - let tables = Box::new(make_tables()); - let v0 = get_edges_from_tables(&tables); - let v1 = get_edges_via_table_iteration_trait_object(&tables); - assert_eq!(v0, v1); -} - -#[test] -fn test_table_collection_population_iteration_object_safety() { - let tables = Box::new(make_tables()); - let v0 = get_populations_from_tables(&tables); - let v1 = get_populations_via_table_iteration_trait_object(&tables); - assert_eq!(v0, v1); + validate_output_from_tables(tables) } From 6e41fa1160c654777d1a11c5277748b74b10a1c7 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 09:55:59 -0700 Subject: [PATCH 16/29] more test --- tests/test_table_traits.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 87636aa8b..9edc1a878 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -11,6 +11,12 @@ impl IteratorOutput { Self { edges, nodes } } + fn new_from_treeseq(treeseq: &tskit::TreeSequence) -> Self { + let edges = treeseq.tables().edges().iter().collect::>(); + let nodes = treeseq.tables().nodes().iter().collect::>(); + Self { edges, nodes } + } + fn new_from_table_access(access: &T) -> Self where T: tskit::TableAccess, @@ -47,6 +53,17 @@ fn validate_output_from_tables(tables: tskit::TableCollection) { assert_eq!(tables_output, dynamic_output); } +fn validate_output_from_treeseq(treeseq: tskit::TreeSequence) { + let treeseq_output = IteratorOutput::new_from_treeseq(&treeseq); + let access_output = IteratorOutput::new_from_table_access(&treeseq); + assert_eq!(treeseq_output, access_output); + let iteration_output = IteratorOutput::new_from_table_iteration(&treeseq); + assert_eq!(treeseq_output, iteration_output); + let boxed = Box::new(treeseq); + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(treeseq_output, dynamic_output); +} + fn make_tables() -> tskit::TableCollection { let mut tables = tskit::TableCollection::new(100.).unwrap(); tables @@ -59,7 +76,7 @@ fn make_tables() -> tskit::TableCollection { .add_node(tskit::NodeFlags::default(), 1.0, -1, -1) .unwrap(); tables.add_edge(0., 50., 1, 0).unwrap(); - tables.add_edge(0., 50., 2, 0).unwrap(); + tables.add_edge(50., 100., 2, 0).unwrap(); tables } @@ -68,3 +85,12 @@ fn test_traits_with_table_collection() { let tables = make_tables(); validate_output_from_tables(tables) } + +#[test] +fn test_traits_with_tree_sequence() { + let mut tables = make_tables(); + tables.full_sort(tskit::TableSortOptions::default()).unwrap(); + tables.build_index().unwrap(); + let treeseq = tskit::TreeSequence::try_from(tables).unwrap(); + validate_output_from_treeseq(treeseq) +} From f468e84dae5d31a0e2daf15af09e1daf16c6e8f7 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 10:21:11 -0700 Subject: [PATCH 17/29] boil more plates --- tests/test_table_traits.rs | 73 ++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 9edc1a878..8c29d8006 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -2,19 +2,45 @@ struct IteratorOutput { edges: Vec, nodes: Vec, + sites: Vec, + mutations: Vec, + migrations: Vec, + populations: Vec, } impl IteratorOutput { fn new_from_tables(tables: &tskit::TableCollection) -> Self { let edges = tables.edges().iter().collect::>(); let nodes = tables.nodes().iter().collect::>(); - Self { edges, nodes } + let sites = tables.sites().iter().collect::>(); + let mutations = tables.mutations().iter().collect::>(); + let populations = tables.populations().iter().collect::>(); + let migrations = tables.migrations().iter().collect::>(); + Self { + edges, + nodes, + sites, + mutations, + populations, + migrations, + } } fn new_from_treeseq(treeseq: &tskit::TreeSequence) -> Self { let edges = treeseq.tables().edges().iter().collect::>(); let nodes = treeseq.tables().nodes().iter().collect::>(); - Self { edges, nodes } + let sites = treeseq.tables().sites().iter().collect::>(); + let mutations = treeseq.tables().mutations().iter().collect::>(); + let populations = treeseq.tables().populations().iter().collect::>(); + let migrations = treeseq.tables().migrations().iter().collect::>(); + Self { + edges, + nodes, + sites, + mutations, + populations, + migrations, + } } fn new_from_table_access(access: &T) -> Self @@ -23,7 +49,18 @@ impl IteratorOutput { { let edges = access.edges().iter().collect::>(); let nodes = access.nodes().iter().collect::>(); - Self { edges, nodes } + let sites = access.sites().iter().collect::>(); + let mutations = access.mutations().iter().collect::>(); + let populations = access.populations().iter().collect::>(); + let migrations = access.migrations().iter().collect::>(); + Self { + edges, + nodes, + sites, + mutations, + populations, + migrations, + } } fn new_from_table_iteration(iterator: &T) -> Self @@ -32,13 +69,35 @@ impl IteratorOutput { { let edges = iterator.edges().iter().collect::>(); let nodes = iterator.nodes().iter().collect::>(); - Self { edges, nodes } + let sites = iterator.sites().iter().collect::>(); + let mutations = iterator.mutations().iter().collect::>(); + let populations = iterator.populations().iter().collect::>(); + let migrations = iterator.migrations().iter().collect::>(); + Self { + edges, + nodes, + sites, + mutations, + populations, + migrations, + } } fn new_from_dyn(dynamic: &dyn tskit::ObjectSafeTableIteration) -> Self { let edges = dynamic.edges().iter().collect::>(); let nodes = dynamic.nodes().iter().collect::>(); - Self { edges, nodes } + let sites = dynamic.sites().iter().collect::>(); + let mutations = dynamic.mutations().iter().collect::>(); + let populations = dynamic.populations().iter().collect::>(); + let migrations = dynamic.migrations().iter().collect::>(); + Self { + edges, + nodes, + sites, + mutations, + populations, + migrations, + } } } @@ -89,7 +148,9 @@ fn test_traits_with_table_collection() { #[test] fn test_traits_with_tree_sequence() { let mut tables = make_tables(); - tables.full_sort(tskit::TableSortOptions::default()).unwrap(); + tables + .full_sort(tskit::TableSortOptions::default()) + .unwrap(); tables.build_index().unwrap(); let treeseq = tskit::TreeSequence::try_from(tables).unwrap(); validate_output_from_treeseq(treeseq) From ae4feeeee37638bd9b078b5840c7fc7a446f9c0c Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 11:00:24 -0700 Subject: [PATCH 18/29] more setup --- tests/test_table_traits.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 8c29d8006..f3e268f23 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -136,6 +136,8 @@ fn make_tables() -> tskit::TableCollection { .unwrap(); tables.add_edge(0., 50., 1, 0).unwrap(); tables.add_edge(50., 100., 2, 0).unwrap(); + let site = tables.add_site(0.25, None).unwrap(); + tables.add_mutation(site, 1, -1, 2.0, None).unwrap(); tables } From da4807c8a4131febdf9532430772c851e496b995 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 11:03:24 -0700 Subject: [PATCH 19/29] more setup --- tests/test_table_traits.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index f3e268f23..8f50da59e 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -125,14 +125,16 @@ fn validate_output_from_treeseq(treeseq: tskit::TreeSequence) { fn make_tables() -> tskit::TableCollection { let mut tables = tskit::TableCollection::new(100.).unwrap(); + let pop0 = tables.add_population().unwrap(); + let pop1 = tables.add_population().unwrap(); tables - .add_node(tskit::NodeFlags::default(), 0.0, -1, -1) + .add_node(tskit::NodeFlags::default(), 0.0, pop1, -1) .unwrap(); tables - .add_node(tskit::NodeFlags::default(), 1.0, -1, -1) + .add_node(tskit::NodeFlags::default(), 1.0, pop0, -1) .unwrap(); tables - .add_node(tskit::NodeFlags::default(), 1.0, -1, -1) + .add_node(tskit::NodeFlags::default(), 1.0, pop1, -1) .unwrap(); tables.add_edge(0., 50., 1, 0).unwrap(); tables.add_edge(50., 100., 2, 0).unwrap(); From 5eca7bea884eb29fe756b0a8cf795e0a00f93ebb Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 14:46:24 -0700 Subject: [PATCH 20/29] fill in tests --- src/traits.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index 619afe121..942bba19c 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -239,15 +239,15 @@ where } fn sites(&self) -> &crate::SiteTable { - todo!() + self.as_ref().sites() } fn mutations(&self) -> &crate::MutationTable { - todo!() + self.as_ref().mutations() } fn populations(&self) -> &crate::PopulationTable { - todo!() + self.as_ref().populations() } } From 1ad35246df8ffef8e7a5c05f566b252b05b77967 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 16:01:51 -0700 Subject: [PATCH 21/29] do we need this --- src/traits.rs | 1 + tests/test_table_traits.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/traits.rs b/src/traits.rs index 942bba19c..b8d2bd637 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -209,6 +209,7 @@ where } fn edges(&self) -> &crate::EdgeTable { + todo!("can only trigger with with Box<&thing that impl TableAccess>"); T::edges(self) } fn sites(&self) -> &crate::SiteTable { diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 8f50da59e..c5546bc45 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -101,6 +101,10 @@ impl IteratorOutput { } } +struct TablesHolder<'tables> { + tables: &'tables tskit::TableCollection, +} + fn validate_output_from_tables(tables: tskit::TableCollection) { let tables_output = IteratorOutput::new_from_tables(&tables); let access_output = IteratorOutput::new_from_table_access(&tables); @@ -149,6 +153,20 @@ fn test_traits_with_table_collection() { validate_output_from_tables(tables) } +#[test] +fn test_traits_with_table_collection_holder() { + let tables = make_tables(); + let tref = &tables; + let tables_output = IteratorOutput::new_from_tables(tref); + let access_output = IteratorOutput::new_from_table_access(tref); + assert_eq!(tables_output, access_output); + let iteration_output = IteratorOutput::new_from_table_iteration(tref); + assert_eq!(tables_output, iteration_output); + let boxed = Box::new(tref); + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(tables_output, dynamic_output); +} + #[test] fn test_traits_with_tree_sequence() { let mut tables = make_tables(); From 276b3e84b9e308fb6b49024dacae22abc22f6177 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 16:19:28 -0700 Subject: [PATCH 22/29] streamline --- tests/test_table_traits.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index c5546bc45..752aa4b7e 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -158,10 +158,6 @@ fn test_traits_with_table_collection_holder() { let tables = make_tables(); let tref = &tables; let tables_output = IteratorOutput::new_from_tables(tref); - let access_output = IteratorOutput::new_from_table_access(tref); - assert_eq!(tables_output, access_output); - let iteration_output = IteratorOutput::new_from_table_iteration(tref); - assert_eq!(tables_output, iteration_output); let boxed = Box::new(tref); let dynamic_output = IteratorOutput::new_from_dyn(&boxed); assert_eq!(tables_output, dynamic_output); From f12503c3e1df1254557a2bd96b5084dbe2c9277f Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 12 Jul 2024 16:26:10 -0700 Subject: [PATCH 23/29] okay, we have other things that we do need to test --- tests/test_table_traits.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 752aa4b7e..3af81ba0e 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -156,11 +156,23 @@ fn test_traits_with_table_collection() { #[test] fn test_traits_with_table_collection_holder() { let tables = make_tables(); - let tref = &tables; + let tref: &tskit::TableCollection = &tables; let tables_output = IteratorOutput::new_from_tables(tref); - let boxed = Box::new(tref); - let dynamic_output = IteratorOutput::new_from_dyn(&boxed); - assert_eq!(tables_output, dynamic_output); + //let boxed = Box::new(tref); + //let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + //assert_eq!(tables_output, dynamic_output); + fn foo(_: impl tskit::TableIteration) { + todo!("this compiles"); + + } + fn foo2(_: T) where T: tskit::TableIteration { + todo!("this compiles"); + } + foo(tref); + foo2(tref); + let h = TablesHolder{tables:tref}; + foo(h.tables); + foo2(h.tables); } #[test] From 2ab435b4265d762cf5110384bfd212f1eed07b13 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Sat, 13 Jul 2024 08:11:58 -0700 Subject: [PATCH 24/29] test new pattern --- src/traits.rs | 1 - tests/test_table_traits.rs | 45 +++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index b8d2bd637..942bba19c 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -209,7 +209,6 @@ where } fn edges(&self) -> &crate::EdgeTable { - todo!("can only trigger with with Box<&thing that impl TableAccess>"); T::edges(self) } fn sites(&self) -> &crate::SiteTable { diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 3af81ba0e..39c867db1 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -63,6 +63,11 @@ impl IteratorOutput { } } + fn new_from_table_access_impl_syntax(access: impl tskit::TableAccess) -> Self + { + Self::new_from_table_access(&access) + } + fn new_from_table_iteration(iterator: &T) -> Self where T: tskit::TableIteration, @@ -101,10 +106,6 @@ impl IteratorOutput { } } -struct TablesHolder<'tables> { - tables: &'tables tskit::TableCollection, -} - fn validate_output_from_tables(tables: tskit::TableCollection) { let tables_output = IteratorOutput::new_from_tables(&tables); let access_output = IteratorOutput::new_from_table_access(&tables); @@ -116,6 +117,22 @@ fn validate_output_from_tables(tables: tskit::TableCollection) { assert_eq!(tables_output, dynamic_output); } +fn validate_output_from_table_ref(tables: tskit::TableCollection) { + let tref = &tables; + let tables_output = IteratorOutput::new_from_tables(tref); + let access_output = IteratorOutput::new_from_table_access(tref); + assert_eq!(tables_output, access_output); + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(tref); + assert_eq!(tables_output, impl_syntax_output); + let iteration_output = IteratorOutput::new_from_table_iteration(tref); + assert_eq!(tables_output, iteration_output); + let boxed = Box::new(tref); + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(tables_output, dynamic_output); + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&boxed); + assert_eq!(tables_output, impl_syntax_output); +} + fn validate_output_from_treeseq(treeseq: tskit::TreeSequence) { let treeseq_output = IteratorOutput::new_from_treeseq(&treeseq); let access_output = IteratorOutput::new_from_table_access(&treeseq); @@ -154,25 +171,9 @@ fn test_traits_with_table_collection() { } #[test] -fn test_traits_with_table_collection_holder() { +fn test_traits_with_table_collection_ref() { let tables = make_tables(); - let tref: &tskit::TableCollection = &tables; - let tables_output = IteratorOutput::new_from_tables(tref); - //let boxed = Box::new(tref); - //let dynamic_output = IteratorOutput::new_from_dyn(&boxed); - //assert_eq!(tables_output, dynamic_output); - fn foo(_: impl tskit::TableIteration) { - todo!("this compiles"); - - } - fn foo2(_: T) where T: tskit::TableIteration { - todo!("this compiles"); - } - foo(tref); - foo2(tref); - let h = TablesHolder{tables:tref}; - foo(h.tables); - foo2(h.tables); + validate_output_from_table_ref(tables) } #[test] From be78c812f41e9946abdf6dbfc3146b280edb4903 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Sat, 13 Jul 2024 08:24:30 -0700 Subject: [PATCH 25/29] one down --- tests/test_table_traits.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 39c867db1..c551e3195 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -63,8 +63,7 @@ impl IteratorOutput { } } - fn new_from_table_access_impl_syntax(access: impl tskit::TableAccess) -> Self - { + fn new_from_table_access_impl_syntax(access: impl tskit::TableAccess) -> Self { Self::new_from_table_access(&access) } @@ -112,9 +111,15 @@ fn validate_output_from_tables(tables: tskit::TableCollection) { assert_eq!(tables_output, access_output); let iteration_output = IteratorOutput::new_from_table_iteration(&tables); assert_eq!(tables_output, iteration_output); + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&tables); + assert_eq!(tables_output, impl_syntax_output); let boxed = Box::new(tables); let dynamic_output = IteratorOutput::new_from_dyn(&boxed); assert_eq!(tables_output, dynamic_output); + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&boxed); + assert_eq!(tables_output, impl_syntax_output); + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); + assert_eq!(tables_output, impl_syntax_output); } fn validate_output_from_table_ref(tables: tskit::TableCollection) { @@ -142,6 +147,11 @@ fn validate_output_from_treeseq(treeseq: tskit::TreeSequence) { let boxed = Box::new(treeseq); let dynamic_output = IteratorOutput::new_from_dyn(&boxed); assert_eq!(treeseq_output, dynamic_output); + todo!("test impl syntax") +} + +fn validate_output_from_treeseq_ref(treeseq: tskit::TreeSequence) { + todo!() } fn make_tables() -> tskit::TableCollection { @@ -186,3 +196,14 @@ fn test_traits_with_tree_sequence() { let treeseq = tskit::TreeSequence::try_from(tables).unwrap(); validate_output_from_treeseq(treeseq) } + +#[test] +fn test_traits_with_tree_sequence_ref() { + let mut tables = make_tables(); + tables + .full_sort(tskit::TableSortOptions::default()) + .unwrap(); + tables.build_index().unwrap(); + let treeseq = tskit::TreeSequence::try_from(tables).unwrap(); + validate_output_from_treeseq_ref(treeseq) +} From 0db081a4abeaf7c1e2df7980bb1002be6035119d Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Sat, 13 Jul 2024 09:58:56 -0700 Subject: [PATCH 26/29] done --- tests/test_table_traits.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index c551e3195..4b23505e9 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -144,14 +144,29 @@ fn validate_output_from_treeseq(treeseq: tskit::TreeSequence) { assert_eq!(treeseq_output, access_output); let iteration_output = IteratorOutput::new_from_table_iteration(&treeseq); assert_eq!(treeseq_output, iteration_output); + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&treeseq); + assert_eq!(treeseq_output, impl_syntax_output); let boxed = Box::new(treeseq); let dynamic_output = IteratorOutput::new_from_dyn(&boxed); assert_eq!(treeseq_output, dynamic_output); - todo!("test impl syntax") + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); + assert_eq!(treeseq_output, impl_syntax_output); } fn validate_output_from_treeseq_ref(treeseq: tskit::TreeSequence) { - todo!() + let treeseq_ref = &treeseq; + let treeseq_output = IteratorOutput::new_from_treeseq(treeseq_ref); + let access_output = IteratorOutput::new_from_table_access(treeseq_ref); + assert_eq!(treeseq_output, access_output); + let iteration_output = IteratorOutput::new_from_table_iteration(treeseq_ref); + assert_eq!(treeseq_output, iteration_output); + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(treeseq_ref); + assert_eq!(treeseq_output, impl_syntax_output); + let boxed = Box::new(treeseq_ref); + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(treeseq_output, dynamic_output); + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); + assert_eq!(treeseq_output, impl_syntax_output); } fn make_tables() -> tskit::TableCollection { From 81c8bc59979182bab1c809bd6483a4ec3c2c065e Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Sat, 13 Jul 2024 10:07:54 -0700 Subject: [PATCH 27/29] scopes --- tests/test_table_traits.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 4b23505e9..bc745b88e 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -107,19 +107,31 @@ impl IteratorOutput { fn validate_output_from_tables(tables: tskit::TableCollection) { let tables_output = IteratorOutput::new_from_tables(&tables); - let access_output = IteratorOutput::new_from_table_access(&tables); - assert_eq!(tables_output, access_output); - let iteration_output = IteratorOutput::new_from_table_iteration(&tables); - assert_eq!(tables_output, iteration_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&tables); - assert_eq!(tables_output, impl_syntax_output); + { + let access_output = IteratorOutput::new_from_table_access(&tables); + assert_eq!(tables_output, access_output); + } + { + let iteration_output = IteratorOutput::new_from_table_iteration(&tables); + assert_eq!(tables_output, iteration_output); + } + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&tables); + assert_eq!(tables_output, impl_syntax_output); + } let boxed = Box::new(tables); - let dynamic_output = IteratorOutput::new_from_dyn(&boxed); - assert_eq!(tables_output, dynamic_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&boxed); - assert_eq!(tables_output, impl_syntax_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); - assert_eq!(tables_output, impl_syntax_output); + { + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(tables_output, dynamic_output); + } + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&boxed); + assert_eq!(tables_output, impl_syntax_output); + } + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); + assert_eq!(tables_output, impl_syntax_output); + } } fn validate_output_from_table_ref(tables: tskit::TableCollection) { From 41aa0b57875add21dce22034198cc29c84f22436 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Sun, 14 Jul 2024 11:53:29 -0700 Subject: [PATCH 28/29] rewrite test to test the prelude contents --- tests/test_table_traits.rs | 92 +++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index bc745b88e..5bf92af76 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -1,3 +1,5 @@ +use tskit::prelude::*; + #[derive(PartialEq, Debug)] struct IteratorOutput { edges: Vec, @@ -63,13 +65,13 @@ impl IteratorOutput { } } - fn new_from_table_access_impl_syntax(access: impl tskit::TableAccess) -> Self { + fn new_from_table_access_impl_syntax(access: impl TableAccess) -> Self { Self::new_from_table_access(&access) } fn new_from_table_iteration(iterator: &T) -> Self where - T: tskit::TableIteration, + T: TableIteration, { let edges = iterator.edges().iter().collect::>(); let nodes = iterator.nodes().iter().collect::>(); @@ -137,48 +139,74 @@ fn validate_output_from_tables(tables: tskit::TableCollection) { fn validate_output_from_table_ref(tables: tskit::TableCollection) { let tref = &tables; let tables_output = IteratorOutput::new_from_tables(tref); - let access_output = IteratorOutput::new_from_table_access(tref); - assert_eq!(tables_output, access_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(tref); - assert_eq!(tables_output, impl_syntax_output); - let iteration_output = IteratorOutput::new_from_table_iteration(tref); - assert_eq!(tables_output, iteration_output); + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(tref); + assert_eq!(tables_output, impl_syntax_output); + } + { + let iteration_output = IteratorOutput::new_from_table_iteration(tref); + assert_eq!(tables_output, iteration_output); + } let boxed = Box::new(tref); - let dynamic_output = IteratorOutput::new_from_dyn(&boxed); - assert_eq!(tables_output, dynamic_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&boxed); - assert_eq!(tables_output, impl_syntax_output); + { + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(tables_output, dynamic_output); + } + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&boxed); + assert_eq!(tables_output, impl_syntax_output); + } } fn validate_output_from_treeseq(treeseq: tskit::TreeSequence) { let treeseq_output = IteratorOutput::new_from_treeseq(&treeseq); - let access_output = IteratorOutput::new_from_table_access(&treeseq); - assert_eq!(treeseq_output, access_output); - let iteration_output = IteratorOutput::new_from_table_iteration(&treeseq); - assert_eq!(treeseq_output, iteration_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&treeseq); - assert_eq!(treeseq_output, impl_syntax_output); + { + let access_output = IteratorOutput::new_from_table_access(&treeseq); + assert_eq!(treeseq_output, access_output); + } + { + let iteration_output = IteratorOutput::new_from_table_iteration(&treeseq); + assert_eq!(treeseq_output, iteration_output); + } + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(&treeseq); + assert_eq!(treeseq_output, impl_syntax_output); + } let boxed = Box::new(treeseq); - let dynamic_output = IteratorOutput::new_from_dyn(&boxed); - assert_eq!(treeseq_output, dynamic_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); - assert_eq!(treeseq_output, impl_syntax_output); + { + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(treeseq_output, dynamic_output); + } + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); + assert_eq!(treeseq_output, impl_syntax_output); + } } fn validate_output_from_treeseq_ref(treeseq: tskit::TreeSequence) { let treeseq_ref = &treeseq; let treeseq_output = IteratorOutput::new_from_treeseq(treeseq_ref); - let access_output = IteratorOutput::new_from_table_access(treeseq_ref); - assert_eq!(treeseq_output, access_output); - let iteration_output = IteratorOutput::new_from_table_iteration(treeseq_ref); - assert_eq!(treeseq_output, iteration_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(treeseq_ref); - assert_eq!(treeseq_output, impl_syntax_output); + { + let access_output = IteratorOutput::new_from_table_access(treeseq_ref); + assert_eq!(treeseq_output, access_output); + } + { + let iteration_output = IteratorOutput::new_from_table_iteration(treeseq_ref); + assert_eq!(treeseq_output, iteration_output); + } + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(treeseq_ref); + assert_eq!(treeseq_output, impl_syntax_output); + } let boxed = Box::new(treeseq_ref); - let dynamic_output = IteratorOutput::new_from_dyn(&boxed); - assert_eq!(treeseq_output, dynamic_output); - let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); - assert_eq!(treeseq_output, impl_syntax_output); + { + let dynamic_output = IteratorOutput::new_from_dyn(&boxed); + assert_eq!(treeseq_output, dynamic_output); + } + { + let impl_syntax_output = IteratorOutput::new_from_table_access_impl_syntax(boxed); + assert_eq!(treeseq_output, impl_syntax_output); + } } fn make_tables() -> tskit::TableCollection { From 64be547524c59033090c1cc25218c48499e48dae Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Mon, 15 Jul 2024 09:08:42 -0700 Subject: [PATCH 29/29] fix tests, fill in impl details --- src/traits.rs | 29 ++++++++++++++++++++++++++++- tests/test_table_traits.rs | 25 +++++++++++++------------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index 942bba19c..536174093 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -133,14 +133,41 @@ pub trait TableIteration: TableAccess { fn edges_iter(&self) -> impl Iterator + '_ { self.edges().iter() } + fn nodes_iter(&self) -> impl Iterator + '_ { + self.nodes().iter() + } + fn sites_iter(&self) -> impl Iterator + '_ { + self.sites().iter() + } + fn mutations_iter(&self) -> impl Iterator + '_ { + self.mutations().iter() + } + fn migrations_iter(&self) -> impl Iterator + '_ { + self.migrations().iter() + } + fn populations_iter(&self) -> impl Iterator + '_ { + self.populations().iter() + } } pub trait ObjectSafeTableIteration: TableAccess { fn edges_iter(&self) -> Box + '_> { Box::new(self.edges().iter()) } + fn nodes_iter(&self) -> Box + '_> { + Box::new(self.nodes().iter()) + } + fn sites_iter(&self) -> Box + '_> { + Box::new(self.sites().iter()) + } + fn mutations_iter(&self) -> Box + '_> { + Box::new(self.mutations().iter()) + } + fn migrations_iter(&self) -> Box + '_> { + Box::new(self.migrations().iter()) + } fn populations_iter(&self) -> Box + '_> { - Box::new(self.populations().iter()) + Box::new(Box::new(self.populations().iter())) } } diff --git a/tests/test_table_traits.rs b/tests/test_table_traits.rs index 5bf92af76..19a31c2db 100644 --- a/tests/test_table_traits.rs +++ b/tests/test_table_traits.rs @@ -73,12 +73,12 @@ impl IteratorOutput { where T: TableIteration, { - let edges = iterator.edges().iter().collect::>(); - let nodes = iterator.nodes().iter().collect::>(); - let sites = iterator.sites().iter().collect::>(); - let mutations = iterator.mutations().iter().collect::>(); - let populations = iterator.populations().iter().collect::>(); - let migrations = iterator.migrations().iter().collect::>(); + let edges = iterator.edges_iter().collect::>(); + let nodes = iterator.nodes_iter().collect::>(); + let sites = iterator.sites_iter().collect::>(); + let mutations = iterator.mutations_iter().collect::>(); + let populations = iterator.populations_iter().collect::>(); + let migrations = iterator.migrations_iter().collect::>(); Self { edges, nodes, @@ -90,12 +90,13 @@ impl IteratorOutput { } fn new_from_dyn(dynamic: &dyn tskit::ObjectSafeTableIteration) -> Self { - let edges = dynamic.edges().iter().collect::>(); - let nodes = dynamic.nodes().iter().collect::>(); - let sites = dynamic.sites().iter().collect::>(); - let mutations = dynamic.mutations().iter().collect::>(); - let populations = dynamic.populations().iter().collect::>(); - let migrations = dynamic.migrations().iter().collect::>(); + let edges_iter: Box + '_> = dynamic.edges_iter(); + let edges = edges_iter.collect::>(); + let nodes = dynamic.nodes_iter().collect::>(); + let sites = dynamic.sites_iter().collect::>(); + let mutations = dynamic.mutations_iter().collect::>(); + let populations = dynamic.populations_iter().collect::>(); + let migrations = dynamic.migrations_iter().collect::>(); Self { edges, nodes,