Skip to content

Commit 8c657c4

Browse files
committed
refactor: NodeFlags no longer based on bitflags
BREAKING CHANGE: pub fns generated by bitflags macro are removed
1 parent a286690 commit 8c657c4

File tree

2 files changed

+89
-58
lines changed

2 files changed

+89
-58
lines changed

src/node_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl NodeTable {
647647
/// of all nodes for which [`crate::NodeFlags::is_sample`]
648648
/// is `true`.
649649
pub fn samples_as_vector(&self) -> Vec<NodeId> {
650-
self.create_node_id_vector(|row| row.flags.contains(NodeFlags::new_sample()))
650+
self.create_node_id_vector(|row| row.flags.contains(NodeFlags::IS_SAMPLE))
651651
}
652652

653653
/// Obtain a vector containing the indexes ("ids") of all nodes

src/sys/flags.rs

Lines changed: 88 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -625,63 +625,72 @@ impl TableIntegrityCheckFlags {
625625
=> check_trees, CHECK_TREES);
626626
}
627627

628-
bitflags! {
629-
/// Node flags
630-
///
631-
/// # Examples
632-
///
633-
/// ## Default (empty) flags
634-
///
635-
/// ```
636-
/// # use tskit::NodeFlags;
637-
/// let f = NodeFlags::default();
638-
/// assert_eq!(f, NodeFlags::NONE);
639-
/// ```
640-
///
641-
/// ## Create a sample node
642-
///
643-
/// Creating a sample node is such a common task that it is supported
644-
/// via a constructor:
645-
///
646-
/// ```
647-
/// # use tskit::NodeFlags;
648-
/// let f = NodeFlags::new_sample();
649-
/// assert_eq!(f, NodeFlags::IS_SAMPLE);
650-
/// ```
651-
///
652-
/// ## Buider API
653-
///
654-
/// These methods can be chained.
655-
///
656-
/// ```
657-
/// # use tskit::NodeFlags;
658-
/// let f = NodeFlags::default().mark_sample();
659-
/// assert_eq!(f, NodeFlags::IS_SAMPLE);
660-
/// ```
661-
#[derive(Default,Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
662-
#[repr(transparent)]
663-
pub struct NodeFlags : RawFlags {
664-
/// Default (empty)
665-
const NONE = 0;
666-
/// Node is a sample
667-
const IS_SAMPLE = ll_bindings::TSK_NODE_IS_SAMPLE;
668-
}
669-
}
628+
//bitflags! {
629+
// /// Node flags
630+
// ///
631+
// /// # Examples
632+
// ///
633+
// /// ## Default (empty) flags
634+
// ///
635+
// /// ```
636+
// /// # use tskit::NodeFlags;
637+
// /// let f = NodeFlags::default();
638+
// /// assert_eq!(f, NodeFlags::NONE);
639+
// /// ```
640+
// ///
641+
// /// ## Create a sample node
642+
// ///
643+
// /// Creating a sample node is such a common task that it is supported
644+
// /// via a constructor:
645+
// ///
646+
// /// ```
647+
// /// # use tskit::NodeFlags;
648+
// /// let f = NodeFlags::new_sample();
649+
// /// assert_eq!(f, NodeFlags::IS_SAMPLE);
650+
// /// ```
651+
// ///
652+
// /// ## Buider API
653+
// ///
654+
// /// These methods can be chained.
655+
// ///
656+
// /// ```
657+
// /// # use tskit::NodeFlags;
658+
// /// let f = NodeFlags::default().mark_sample();
659+
// /// assert_eq!(f, NodeFlags::IS_SAMPLE);
660+
// /// ```
661+
// #[derive(Default,Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
662+
// #[repr(transparent)]
663+
// pub struct NodeFlags : RawFlags {
664+
// /// Default (empty)
665+
// const NONE = 0;
666+
// /// Node is a sample
667+
// const IS_SAMPLE = ll_bindings::TSK_NODE_IS_SAMPLE;
668+
// }
669+
//}
670+
671+
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
672+
pub struct NodeFlags(tsk_flags_t);
670673

671674
impl NodeFlags {
675+
/// Default (empty)
676+
pub const NONE: tsk_flags_t = 0;
677+
/// Node is a sample
678+
pub const IS_SAMPLE: tsk_flags_t = ll_bindings::TSK_NODE_IS_SAMPLE;
679+
672680
/// Create a new flags instance with `IS_SAMPLE` set.
673681
pub fn new_sample() -> Self {
674682
Self::default().mark_sample()
675683
}
676684

677-
flag_builder_api!(
678-
/// Set [`IS_SAMPLE`](crate::NodeFlags::IS_SAMPLE)
679-
///
680-
/// # Note
681-
///
682-
/// This function is called `mark_sample` to not conflict
683-
/// with [`NodeFlags::is_sample`], which predates it.
684-
=> mark_sample, IS_SAMPLE);
685+
/// Set [`IS_SAMPLE`](crate::NodeFlags::IS_SAMPLE)
686+
///
687+
/// # Note
688+
///
689+
/// This function is called `mark_sample` to not conflict
690+
/// with [`NodeFlags::is_sample`], which predates it.
691+
pub fn mark_sample(self) -> Self {
692+
Self(self.0 | Self::IS_SAMPLE)
693+
}
685694

686695
/// We do not enforce valid flags in the library.
687696
/// This function will return `true` if any bits
@@ -693,7 +702,35 @@ impl NodeFlags {
693702
/// Returns `true` if flags contains `IS_SAMPLE`,
694703
/// and `false` otherwise.
695704
pub fn is_sample(&self) -> bool {
696-
self.contains(NodeFlags::IS_SAMPLE)
705+
self.contains(Self::IS_SAMPLE)
706+
}
707+
708+
pub fn bits(&self) -> tsk_flags_t {
709+
self.0
710+
}
711+
712+
pub fn contains<I>(&self, bit: I) -> bool
713+
where
714+
I: Into<Self> + Copy,
715+
{
716+
bit.into().0 == 0 || (self.0 & bit.into().0) != 0
717+
}
718+
719+
pub fn toggle(&mut self, bit: tsk_flags_t) {
720+
self.0 ^= bit
721+
}
722+
723+
pub fn remove<I>(&mut self, bit: I)
724+
where
725+
I: Into<Self>,
726+
{
727+
self.0 &= !bit.into().0
728+
}
729+
}
730+
731+
impl From<tsk_flags_t> for NodeFlags {
732+
fn from(value: tsk_flags_t) -> Self {
733+
Self(value)
697734
}
698735
}
699736

@@ -736,12 +773,6 @@ impl_from_for_flag_types!(IndividualTableSortOptions);
736773
impl_from_for_flag_types!(TableIntegrityCheckFlags);
737774
impl_from_for_flag_types!(TableOutputOptions);
738775

739-
impl From<RawFlags> for NodeFlags {
740-
fn from(flags: RawFlags) -> Self {
741-
Self::from_bits_retain(flags)
742-
}
743-
}
744-
745776
impl From<RawFlags> for IndividualFlags {
746777
fn from(flags: RawFlags) -> Self {
747778
Self::from_bits_retain(flags)

0 commit comments

Comments
 (0)