Skip to content

Commit a9e784c

Browse files
committed
Transition objstr to atomic types
1 parent 8ed8dd9 commit a9e784c

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

vm/src/obj/objstr.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::cell::Cell;
21
use std::char;
32
use std::fmt;
43
use std::mem::size_of;
54
use std::ops::Range;
65
use std::str::FromStr;
76
use std::string::ToString;
87

8+
use crossbeam_utils::atomic::AtomicCell;
99
use num_traits::ToPrimitive;
1010
use unic::ucd::category::GeneralCategory;
1111
use unic::ucd::ident::{is_xid_continue, is_xid_start};
@@ -45,10 +45,10 @@ use crate::vm::VirtualMachine;
4545
/// encoding defaults to sys.getdefaultencoding().
4646
/// errors defaults to 'strict'."
4747
#[pyclass(name = "str")]
48-
#[derive(Clone, Debug)]
48+
#[derive(Debug)]
4949
pub struct PyString {
5050
value: String,
51-
hash: Cell<Option<pyhash::PyHash>>,
51+
hash: AtomicCell<Option<pyhash::PyHash>>,
5252
}
5353

5454
impl PyString {
@@ -68,7 +68,7 @@ impl From<String> for PyString {
6868
fn from(s: String) -> PyString {
6969
PyString {
7070
value: s,
71-
hash: Cell::default(),
71+
hash: AtomicCell::default(),
7272
}
7373
}
7474
}
@@ -97,7 +97,7 @@ impl TryIntoRef<PyString> for &str {
9797
#[derive(Debug)]
9898
pub struct PyStringIterator {
9999
pub string: PyStringRef,
100-
byte_position: Cell<usize>,
100+
byte_position: AtomicCell<usize>,
101101
}
102102

103103
impl PyValue for PyStringIterator {
@@ -110,14 +110,13 @@ impl PyValue for PyStringIterator {
110110
impl PyStringIterator {
111111
#[pymethod(name = "__next__")]
112112
fn next(&self, vm: &VirtualMachine) -> PyResult {
113-
let pos = self.byte_position.get();
113+
let pos = self.byte_position.load();
114114

115115
if pos < self.string.value.len() {
116116
// We can be sure that chars() has a value, because of the pos check above.
117117
let char_ = self.string.value[pos..].chars().next().unwrap();
118118

119-
self.byte_position
120-
.set(self.byte_position.get() + char_.len_utf8());
119+
self.byte_position.store(pos + char_.len_utf8());
121120

122121
char_.to_string().into_pyobject(vm)
123122
} else {
@@ -134,7 +133,7 @@ impl PyStringIterator {
134133
#[pyclass]
135134
#[derive(Debug)]
136135
pub struct PyStringReverseIterator {
137-
pub position: Cell<usize>,
136+
pub position: AtomicCell<usize>,
138137
pub string: PyStringRef,
139138
}
140139

@@ -148,13 +147,12 @@ impl PyValue for PyStringReverseIterator {
148147
impl PyStringReverseIterator {
149148
#[pymethod(name = "__next__")]
150149
fn next(&self, vm: &VirtualMachine) -> PyResult {
151-
if self.position.get() > 0 {
152-
let position: usize = self.position.get() - 1;
150+
let pos = self.position.load();
153151

154-
#[allow(clippy::range_plus_one)]
155-
let value = self.string.value.do_slice(position..position + 1);
152+
if pos > 0 {
153+
let value = self.string.value.do_slice(pos - 1..pos);
156154

157-
self.position.set(position);
155+
self.position.store(pos - 1);
158156
value.into_pyobject(vm)
159157
} else {
160158
Err(objiter::new_stop_iteration(vm))
@@ -311,11 +309,11 @@ impl PyString {
311309

312310
#[pymethod(name = "__hash__")]
313311
fn hash(&self) -> pyhash::PyHash {
314-
match self.hash.get() {
312+
match self.hash.load() {
315313
Some(hash) => hash,
316314
None => {
317315
let hash = pyhash::hash_value(&self.value);
318-
self.hash.set(Some(hash));
316+
self.hash.store(Some(hash));
319317
hash
320318
}
321319
}
@@ -1287,20 +1285,20 @@ impl PyString {
12871285
encode_string(zelf, encoding.into_option(), errors.into_option(), vm)
12881286
}
12891287

1290-
#[pymethod(name = "__iter__")]
1288+
#[pymethod(magic)]
12911289
fn iter(zelf: PyRef<Self>) -> PyStringIterator {
12921290
PyStringIterator {
1293-
byte_position: Cell::new(0),
1291+
byte_position: AtomicCell::new(0),
12941292
string: zelf,
12951293
}
12961294
}
12971295

1298-
#[pymethod(name = "__reversed__")]
1296+
#[pymethod(magic)]
12991297
fn reversed(zelf: PyRef<Self>) -> PyStringReverseIterator {
13001298
let begin = zelf.value.chars().count();
13011299

13021300
PyStringReverseIterator {
1303-
position: Cell::new(begin),
1301+
position: AtomicCell::new(begin),
13041302
string: zelf,
13051303
}
13061304
}

0 commit comments

Comments
 (0)