Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define traits for table access and iteration #649

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
strategy:
matrix:
rust:
- 1.71.0
- 1.75.0
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@v1
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +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 tree_interface::{NodeTraversalOrder, TreeInterface};
pub use trees::{Tree, TreeSequence};

Expand Down
2 changes: 2 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
161 changes: 161 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,164 @@ 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;
fn populations(&self) -> &crate::PopulationTable;
}

pub trait TableIteration: TableAccess {
fn edges_iter(&self) -> impl Iterator<Item = crate::EdgeTableRow> + '_ {
self.edges().iter()
}
fn nodes_iter(&self) -> impl Iterator<Item = crate::NodeTableRow> + '_ {
self.nodes().iter()
}
fn sites_iter(&self) -> impl Iterator<Item = crate::SiteTableRow> + '_ {
self.sites().iter()
}
fn mutations_iter(&self) -> impl Iterator<Item = crate::MutationTableRow> + '_ {
self.mutations().iter()
}
fn migrations_iter(&self) -> impl Iterator<Item = crate::MigrationTableRow> + '_ {
self.migrations().iter()
}
fn populations_iter(&self) -> impl Iterator<Item = crate::PopulationTableRow> + '_ {
self.populations().iter()
}
}

pub trait ObjectSafeTableIteration: TableAccess {
fn edges_iter(&self) -> Box<dyn Iterator<Item = crate::EdgeTableRow> + '_> {
Box::new(self.edges().iter())
}
fn nodes_iter(&self) -> Box<dyn Iterator<Item = crate::NodeTableRow> + '_> {
Box::new(self.nodes().iter())
}
fn sites_iter(&self) -> Box<dyn Iterator<Item = crate::SiteTableRow> + '_> {
Box::new(self.sites().iter())
}
fn mutations_iter(&self) -> Box<dyn Iterator<Item = crate::MutationTableRow> + '_> {
Box::new(self.mutations().iter())
}
fn migrations_iter(&self) -> Box<dyn Iterator<Item = crate::MigrationTableRow> + '_> {
Box::new(self.migrations().iter())
}
fn populations_iter(&self) -> Box<dyn Iterator<Item = crate::PopulationTableRow> + '_> {
Box::new(Box::new(self.populations().iter()))
}
}

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()
}

fn populations(&self) -> &crate::PopulationTable {
self.populations()
}
}

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()
}

fn populations(&self) -> &crate::PopulationTable {
self.tables().populations()
}
}

impl<T> 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)
}
fn populations(&self) -> &crate::PopulationTable {
T::populations(self)
}
}

impl<T> TableAccess for Box<T>
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 {
self.as_ref().nodes()
}

fn sites(&self) -> &crate::SiteTable {
self.as_ref().sites()
}

fn mutations(&self) -> &crate::MutationTable {
self.as_ref().mutations()
}

fn populations(&self) -> &crate::PopulationTable {
self.as_ref().populations()
}
}

impl<T> TableIteration for T where T: TableAccess + ?Sized {}
impl<T> ObjectSafeTableIteration for T where T: TableAccess + ?Sized {}
Loading
Loading