Struct tskit::IndividualTable

source ·
#[repr(transparent)]
pub struct IndividualTable { /* private fields */ }
Expand description

An immutable view of a individual table.

Implementations§

source§

impl IndividualTable

source

pub fn num_rows(&self) -> SizeType

Return the number of rows

source

pub fn flags<I: Into<IndividualId> + Copy>( &self, row: I ) -> Option<IndividualFlags>

Return the flags for a given row.

Returns
  • Some(flags) if row is valid.
  • None otherwise.
source

pub fn location<I: Into<IndividualId> + Copy>( &self, row: I ) -> Option<&[Location]>

Return the locations for a given row.

Returns
  • Some(location) if row is valid.
  • None otherwise.
source

pub fn parents<I: Into<IndividualId> + Copy>( &self, row: I ) -> Option<&[IndividualId]>

Return the parents for a given row.

Returns
  • Some(parents) if row is valid.
  • None otherwise.
source

pub fn metadata<T: IndividualMetadata>( &self, row: IndividualId ) -> Option<Result<T, TskitError>>

Return the metadata for a given row.

Returns
  • Some(Ok(T)) if row is valid and decoding succeeded.
  • Some(Err(_)) if row is not valid and decoding failed.
  • None if row is not valid.
Errors
Examples

For all examples, this is our metadata type. We will add all instances with a value of x = 1.

#[derive(serde::Serialize, serde::Deserialize, tskit::metadata::IndividualMetadata)]
#[serializer("serde_json")]
struct IndividualMetadata {
   x: i32,
}
Without matches
// We know the metadata are here, so we unwrap the Option and the Result:
let decoded = tables.individuals().metadata::<IndividualMetadata>(0.into()).unwrap().unwrap();
assert_eq!(decoded.x, 1);
Checking for errors and absence of metadata

The Option<Result<_>> return value allows all three return possibilities to be easily covered:

match tables.individuals().metadata::<IndividualMetadata>(0.into())
{
    Some(Ok(metadata)) => assert_eq!(metadata.x, 1),
    Some(Err(_)) => panic!("got an error??"),
    None => panic!("Got None??"),
};
Attempting to use the wrong type.

Let’s define a mutation metadata type with the exact same fields as our individual metadata defined above:

#[derive(serde::Serialize, serde::Deserialize, tskit::metadata::MutationMetadata)]
#[serializer("serde_json")]
struct MutationMetadata {
   x: i32,
}

This type has the wrong trait bound and will cause compilation to fail:

match tables.individuals().metadata::<MutationMetadata>(0.into())
{
    Some(Ok(metadata)) => assert_eq!(metadata.x, 1),
    Some(Err(_)) => panic!("got an error??"),
    None => panic!("Got None??"),
};
Limitations: different type, same trait bound

Finally, let us consider a different struct that has identical fields to IndividualMetadata defined above and also implements the correct trait:

#[derive(serde::Serialize, serde::Deserialize, tskit::metadata::IndividualMetadata)]
#[serializer("serde_json")]
struct IndividualMetadataToo {
   x: i32,
}

Let’s walk through a detailed example:

// create a mutable table collection
let mut tables = tskit::TableCollection::new(100.).unwrap();
// Create some metadata based on our FIRST type
let metadata = IndividualMetadata { x: 1 };
// Add a row with our metadata
assert!(tables.add_individual_with_metadata(0, None, None, &metadata).is_ok());
// Trying to fetch using our SECOND type as the generic type works!
match tables.individuals().metadata::<IndividualMetadataToo>(0.into())
{
    Some(Ok(metadata)) => assert_eq!(metadata.x, 1),
    Some(Err(_)) => panic!("got an error??"),
    None => panic!("Got None??"),
};

What is going on here? Both types satisfy the same trait bound (metadata::IndividualMetadata) and their data fields look identical to serde_json. Thus, one is exchangeable for the other because they have the exact same behavior.

However, it is also true that this is (often/usually/always) not exactly what we want. We are experimenting with encapsulation APIs involving traits with associated types to enforce at compile time that exactly one type (struct/enum, etc.) is a valid metadata type for a table.

source

pub fn iter(&self) -> impl Iterator<Item = IndividualTableRow> + '_

Return an iterator over rows of the table. The value of the iterator is IndividualTableRow.

source

pub fn lending_iter(&self) -> IndividualTableRowView<'_>

source

pub fn row<I: Into<IndividualId> + Copy>( &self, r: I ) -> Option<IndividualTableRow>

Return row r of the table.

Parameters
  • r: the row id.
Returns
  • Some(row) if r is valid
  • None otherwise
source

pub fn row_view<I: Into<IndividualId> + Copy>( &self, r: I ) -> Option<IndividualTableRowView<'_>>

Return a view of r of the table.

Parameters
  • r: the row id.
Returns
  • Some(row view) if r is valid
  • None otherwise
source

pub fn flags_slice(&self) -> &[IndividualFlags]

Get the flags column as a slice

source

pub fn flags_slice_raw(&self) -> &[tsk_flags_t]

Get the flags column as a slice

Trait Implementations§

source§

impl Debug for IndividualTable

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Free for T

source§

unsafe default fn free(ptr_ref: NonNull<T>)

Drops the content pointed by this pointer and frees it. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.