| 1499 | } |
| 1500 | |
| 1501 | pub fn concat_in_place(&mut self, other: &Wtf8, vm: &VirtualMachine) { |
| 1502 | if other.is_empty() { |
| 1503 | return; |
| 1504 | } |
| 1505 | let mut s = Wtf8Buf::with_capacity(self.byte_len() + other.len()); |
| 1506 | s.push_wtf8(self.as_ref()); |
| 1507 | s.push_wtf8(other); |
| 1508 | if self.as_object().strong_count() == 1 { |
| 1509 | // SAFETY: strong_count()==1 guarantees unique ownership of this PyStr. |
| 1510 | // Mutating payload in place preserves semantics while avoiding PyObject reallocation. |
| 1511 | unsafe { |
| 1512 | let payload = self.payload() as *const PyStr as *mut PyStr; |
| 1513 | (*payload).data = PyStr::from(s).data; |
| 1514 | (*payload) |
| 1515 | .hash |
| 1516 | .store(hash::SENTINEL, atomic::Ordering::Relaxed); |
| 1517 | } |
| 1518 | } else { |
| 1519 | *self = PyStr::from(s).into_ref(&vm.ctx); |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | pub fn try_into_utf8(self, vm: &VirtualMachine) -> PyResult<PyRef<PyUtf8Str>> { |
| 1524 | self.ensure_valid_utf8(vm)?; |