Visitar URL original
compiler-core: implement DerefMut and IndexMut for Constants by youknowone · Pull Request #8557 · RustPython/RustPython · GitHub
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
compiler-core: implement DerefMut and IndexMut for Constants
`Constants<C>` exposed `Deref` and `Index<ConstIdx>` but neither mutable
counterpart, so an owner of a `CodeObject` could read a constant but not
replace one in place and had to rebuild the whole boxed slice.

`[T]` already carries both halves of the `VarNum` pair in this file.

Assisted-by: Claude
  • Loading branch information
youknowone committed Aug 19, 2026
commit 4349d4a66f218308a05858bc7fcdd61fbf416ff4
14 changes: 13 additions & 1 deletion crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bitflags::bitflags;
use core::{
cell::UnsafeCell,
hash, mem,
ops::{Deref, Index, IndexMut},
ops::{Deref, DerefMut, Index, IndexMut},
sync::atomic::{AtomicU8, AtomicU16, AtomicUsize, Ordering},
};
use itertools::Itertools;
Expand Down Expand Up @@ -383,6 +383,12 @@ impl<C: Constant> Deref for Constants<C> {
}
}

impl<C: Constant> DerefMut for Constants<C> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<C: Constant> Index<oparg::ConstIdx> for Constants<C> {
type Output = C;

Expand All @@ -391,6 +397,12 @@ impl<C: Constant> Index<oparg::ConstIdx> for Constants<C> {
}
}

impl<C: Constant> IndexMut<oparg::ConstIdx> for Constants<C> {
fn index_mut(&mut self, consti: oparg::ConstIdx) -> &mut Self::Output {
&mut self.0[consti.as_usize()]
}
}

impl<C: Constant> FromIterator<C> for Constants<C> {
fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self {
Self(iter.into_iter().collect())
Expand Down
Loading