Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
feat: TableViews can now lend &mut for all table types.
  • Loading branch information
molpopgen committed Jan 26, 2023
commit 04ccd96523f60e0c4af15112b921c5d74fd01945
37 changes: 37 additions & 0 deletions src/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,29 @@ impl TableCollection {
to self.views {
/// Get mutable reference to the [``NodeTable``](crate::NodeTable).
pub fn nodes_mut(&mut self) -> &mut crate::NodeTable;

/// Get mutable reference to the [``EdgeTable``](crate::EdgeTable).
pub fn edges_mut(&mut self) -> &mut crate::EdgeTable;

/// Get mutable reference to the [``SiteTable``](crate::SiteTable).
pub fn sites_mut(&mut self) -> &mut crate::SiteTable;

/// Get mutable reference to the [``MutationTable``](crate::MutationTable).
pub fn mutations_mut(&mut self) -> &mut crate::MutationTable;

/// Get mutable reference to the [``IndividualTable``](crate::IndividualTable).
pub fn individuals_mut(&mut self) -> &mut crate::IndividualTable;

/// Get mutable reference to the [``PopulationTable``](crate::PopulationTable).
pub fn populations_mut(&mut self) -> &mut crate::PopulationTable;

/// Get mutable reference to the [``MigrationTable``](crate::PopulationTable).
pub fn migrations_mut(&mut self) -> &mut crate::MigrationTable;

#[cfg(feature="provenance")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "provenance")))]
/// Get mutable reference to the [``ProvenanceTable``](crate::ProvenanceTable).
pub fn provenances_mut(&mut self) -> &mut crate::provenance::ProvenanceTable;
}
}

Expand All @@ -1259,3 +1282,17 @@ impl TableCollection {
&mut *self.inner
}
}

#[test]
fn test_mutable_node_references() {
let mut tables = crate::TableCollection::new(10.0).unwrap();
let _nodes: &mut crate::NodeTable = tables.nodes_mut();
let _edges: &mut crate::EdgeTable = tables.edges_mut();
let _sites: &mut crate::SiteTable = tables.sites_mut();
let _mutations: &mut crate::MutationTable = tables.mutations_mut();
let _individuals: &mut crate::IndividualTable = tables.individuals_mut();
let _populations: &mut crate::PopulationTable = tables.populations_mut();
let _migrations: &mut crate::MigrationTable = tables.migrations_mut();
#[cfg(feature = "provenance")]
let _provenances: &mut crate::provenance::ProvenanceTable = tables.provenances_mut();
}
37 changes: 37 additions & 0 deletions src/table_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ impl TableViews {
&self.edges
}

/// Get mutable reference to the [``EdgeTable``](crate::EdgeTable).
pub fn edges_mut(&mut self) -> &mut EdgeTable {
&mut self.edges
}

/// Get reference to the [``NodeTable``](crate::NodeTable).
pub fn nodes(&self) -> &NodeTable {
&self.nodes
Expand All @@ -105,33 +110,65 @@ impl TableViews {
&self.sites
}

/// Get mutable reference to the [``SiteTable``](crate::SiteTable).
pub fn sites_mut(&mut self) -> &mut SiteTable {
&mut self.sites
}

/// Get reference to the [``MutationTable``](crate::MutationTable).
pub fn mutations(&self) -> &MutationTable {
&self.mutations
}

/// Get mutable reference to the [``MutationTable``](crate::MutationTable).
pub fn mutations_mut(&mut self) -> &mut MutationTable {
&mut self.mutations
}

/// Get reference to the [``IndividualTable``](crate::IndividualTable).
pub fn individuals(&self) -> &IndividualTable {
&self.individuals
}

/// Get mutable reference to the [``IndividualTable``](crate::IndividualTable).
pub fn individuals_mut(&mut self) -> &mut IndividualTable {
&mut self.individuals
}

/// Get reference to the [``PopulationTable``](crate::PopulationTable).
pub fn populations(&self) -> &PopulationTable {
&self.populations
}

/// Get mutable reference to the [``PopulationTable``](crate::PopulationTable).
pub fn populations_mut(&mut self) -> &mut PopulationTable {
&mut self.populations
}

/// Get reference to the [``MigrationTable``](crate::MigrationTable).
pub fn migrations(&self) -> &MigrationTable {
&self.migrations
}

/// Get mutable reference to the [``MigrationTable``](crate::MigrationTable).
pub fn migrations_mut(&mut self) -> &mut MigrationTable {
&mut self.migrations
}

#[cfg(feature = "provenance")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "provenance")))]
/// Get reference to the [``ProvenanceTable``](crate::provenance::ProvenanceTable)
pub fn provenances(&self) -> &ProvenanceTable {
&self.provenances
}

#[cfg(feature = "provenance")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "provenance")))]
/// Get mutable reference to the [``ProvenanceTable``](crate::provenance::ProvenanceTable)
pub fn provenances_mut(&mut self) -> &mut ProvenanceTable {
&mut self.provenances
}

/// Return an iterator over the edges.
pub fn edges_iter(&self) -> impl Iterator<Item = crate::EdgeTableRow> + '_ {
self.edges.iter()
Expand Down