impl sequence protocol for PyFrozenSet
@@ -729,7 +729,10 @@ impl Constructor for PyFrozenSet {
}
}
-#[pyimpl(flags(BASETYPE), with(Hashable, Comparable, Iterable, Constructor))]
+#[pyimpl(
+ flags(BASETYPE),
+ with(AsSequence, Hashable, Comparable, Iterable, Constructor)
+)]
impl PyFrozenSet {
// Also used by ssl.rs windows.
pub fn from_iter(
@@ -891,6 +894,23 @@ impl PyFrozenSet {
}
}
+impl AsSequence for PyFrozenSet {
+ fn as_sequence(
+ _zelf: &crate::PyObjectView<Self>,
+ _vm: &VirtualMachine,
+ ) -> Cow<'static, PySequenceMethods> {
+ Cow::Borrowed(&Self::SEQUENCE_METHODS)
+ }
+}
+
+impl PyFrozenSet {
+ const SEQUENCE_METHODS: PySequenceMethods = PySequenceMethods {
+ length: Some(|seq, _vm| Ok(seq.obj_as::<Self>().len())),
+ contains: Some(|seq, needle, vm| seq.obj_as::<Self>().inner.contains(needle, vm)),
+ ..*PySequenceMethods::not_implemented()
+ };
+}
+
impl Hashable for PyFrozenSet {
#[inline]
fn hash(zelf: &crate::PyObjectView<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
