From 42ba492b880067eab51f5ec077349eabd0cf9e87 Mon Sep 17 00:00:00 2001 From: Mel Henning Date: Mon, 2 Jun 2025 12:58:31 -0400 Subject: [PATCH] compiler/rust/bitset: BitSetStream takes Key type This was an oversight when BitSet was parameterized on a key type. BitSetStream needs to also take a key type to prevent users from mixing different key types in binary operators. Constraining this makes BitSet usage more type safe. Part-of: --- src/compiler/rust/bitset.rs | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/compiler/rust/bitset.rs b/src/compiler/rust/bitset.rs index a5226a1fb4c..f0327893b29 100644 --- a/src/compiler/rust/bitset.rs +++ b/src/compiler/rust/bitset.rs @@ -223,7 +223,7 @@ fn every_nth_bit(n: usize) -> u32 { impl BitSet { /// Evaluate an expression and store its value in self - pub fn assign(&mut self, value: BitSetStream) + pub fn assign(&mut self, value: BitSetStream) where B: BitSetStreamTrait, { @@ -242,7 +242,7 @@ impl BitSet { /// Returns true if the value of self changes, or false otherwise. If you /// don't need the return value of this function, consider using the `|=` /// operator instead. - pub fn union_with(&mut self, other: BitSetStream) -> bool + pub fn union_with(&mut self, other: BitSetStream) -> bool where B: BitSetStreamTrait, { @@ -263,10 +263,13 @@ impl BitSet { pub fn s<'a>( &'a self, _: RangeFull, - ) -> BitSetStream { - BitSetStream(BitSetStreamFromBitSet { - iter: self.words.iter().copied(), - }) + ) -> BitSetStream { + BitSetStream( + BitSetStreamFromBitSet { + iter: self.words.iter().copied(), + }, + PhantomData, + ) } } @@ -318,15 +321,15 @@ where } } -pub struct BitSetStream(T) +pub struct BitSetStream(T, PhantomData) where T: BitSetStreamTrait; -impl From> for BitSet +impl From> for BitSet where T: BitSetStreamTrait, { - fn from(value: BitSetStream) -> Self { + fn from(value: BitSetStream) -> Self { let mut out = BitSet::new(); out.assign(value); out @@ -371,26 +374,29 @@ macro_rules! binop { } } - impl $BinOp> for BitSetStream + impl $BinOp> for BitSetStream where A: BitSetStreamTrait, B: BitSetStreamTrait, { - type Output = BitSetStream<$Struct>; + type Output = BitSetStream<$Struct, K>; - fn $bin_op(self, rhs: BitSetStream) -> Self::Output { - BitSetStream($Struct { - a: self.0, - b: rhs.0, - }) + fn $bin_op(self, rhs: BitSetStream) -> Self::Output { + BitSetStream( + $Struct { + a: self.0, + b: rhs.0, + }, + PhantomData, + ) } } - impl $AssignBinOp> for BitSet + impl $AssignBinOp> for BitSet where B: BitSetStreamTrait, { - fn $assign_bin_op(&mut self, rhs: BitSetStream) { + fn $assign_bin_op(&mut self, rhs: BitSetStream) { let mut rhs = rhs.0; let $a_len = self.words.len();