Repository: RustPython/RustPython
File: crates/vm/src/builtins/set.rs (Rust)

Code before:
        let s = if inner.len() == 0 {
            format!("{class_name}()")
        } else if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
            inner.repr(Some(&class_name), vm)?
        } else {
            format!("{class_name}(...)")
        };
        Ok(s)
    }
}

struct AnySet {
    object: PyObjectRef,
}

impl AnySet {
    /// Check if object is a set or frozenset (including subclasses)
    /// Equivalent to CPython's PyAnySet_Check
    fn check(obj: &PyObject, vm: &VirtualMachine) -> bool {
        let ctx = &vm.ctx;
        obj.fast_isinstance(ctx.types.set_type) || obj.fast_isinstance(ctx.types.frozenset_type)
    }

    fn is_same_object(&self, other: &PyObject) -> bool {
        self.object.is(other)
    }

    fn into_iterable(self, vm: &VirtualMachine) -> PyResult<ArgIterable> {
        self.object.try_into_value(vm)
    }

    fn into_iterable_iter(
        self,
        vm: &VirtualMachine,
    ) -> PyResult<impl core::iter::Iterator<Item = ArgIterable>> {
        Ok(core::iter::once(self.into_iterable(vm)?))
    }

    fn as_inner(&self) -> &PySetInner {
        match_class!(match self.object.as_object() {
            ref set @ PySet => &set.inner,
            ref frozen @ PyFrozenSet => &frozen.inner,
            _ => unreachable!("AnySet is always PySet or PyFrozenSet"), // should not be called.
        })
    }
}

impl TryFromObject for AnySet {
    fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
        let class = obj.class();
        if class.fast_issubclass(vm.ctx.types.set_type)

Review comment (suggestion):
We have a mechanism to do this.

`is` is not a method of `PyObject`, but an object of`AsObject`. And PyObject is implementing AsObject through Borrow<PyObject>.

AnySet can do the same thing.
`impl Borrow<PyObject> for AnySet {}`
will enable AsObject for AnySet. Then `set.is_same_object(zelf.as_object())` above can be simply written as `set.is(zelf.as_object())`

Code after:
        let class_name = class.name();
        let s = if inner.len() == 0 {
            format!("{class_name}()")
        } else if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
            inner.repr(Some(&class_name), vm)?
        } else {
            format!("{class_name}(...)")
        };
        Ok(s)
    }
}

struct AnySet {
    object: PyObjectRef,
}

impl Borrow<PyObject> for AnySet {
    #[inline(always)]
    fn borrow(&self) -> &PyObject {
        &self.object
    }
}

impl AnySet {
    /// Check if object is a set or frozenset (including subclasses)
    /// Equivalent to CPython's PyAnySet_Check
    fn check(obj: &PyObject, vm: &VirtualMachine) -> bool {
        let ctx = &vm.ctx;
        obj.fast_isinstance(ctx.types.set_type) || obj.fast_isinstance(ctx.types.frozenset_type)
    }

    fn into_iterable(self, vm: &VirtualMachine) -> PyResult<ArgIterable> {
        self.object.try_into_value(vm)
    }

    fn into_iterable_iter(
        self,
        vm: &VirtualMachine,
    ) -> PyResult<impl core::iter::Iterator<Item = ArgIterable>> {
        Ok(core::iter::once(self.into_iterable(vm)?))
    }

    fn as_inner(&self) -> &PySetInner {
        match_class!(match self.object.as_object() {
            ref set @ PySet => &set.inner,
            ref frozen @ PyFrozenSet => &frozen.inner,
            _ => unreachable!("AnySet is always PySet or PyFrozenSet"), // should not be called.
        })
    }
}