Visitar URL original
compiler-core: implement DerefMut and IndexMut for Constants (#8557) · RustPython/RustPython@91a725f · GitHub
Skip to content

Commit 91a725f

Browse files
authored
compiler-core: implement DerefMut and IndexMut for Constants (#8557)
`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
1 parent 86d9407 commit 91a725f

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎crates/compiler-core/src/bytecode.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bitflags::bitflags;
1111
use core::{
1212
cell::UnsafeCell,
1313
hash, mem,
14-
ops::{Deref, Index, IndexMut},
14+
ops::{Deref, DerefMut, Index, IndexMut},
1515
sync::atomic::{AtomicU8, AtomicU16, AtomicUsize, Ordering},
1616
};
1717
use itertools::Itertools;
@@ -383,6 +383,12 @@ impl<C: Constant> Deref for Constants<C> {
383383
}
384384
}
385385

386+
impl<C: Constant> DerefMut for Constants<C> {
387+
fn deref_mut(&mut self) -> &mut Self::Target {
388+
&mut self.0
389+
}
390+
}
391+
386392
impl<C: Constant> Index<oparg::ConstIdx> for Constants<C> {
387393
type Output = C;
388394

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

400+
impl<C: Constant> IndexMut<oparg::ConstIdx> for Constants<C> {
401+
fn index_mut(&mut self, consti: oparg::ConstIdx) -> &mut Self::Output {
402+
&mut self.0[consti.as_usize()]
403+
}
404+
}
405+
394406
impl<C: Constant> FromIterator<C> for Constants<C> {
395407
fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self {
396408
Self(iter.into_iter().collect())

0 commit comments

Comments
 (0)