Skip to content

Commit 58ac82b

Browse files
committed
Switch hacky GAT-workarounds to use actual GATs
1 parent 166959d commit 58ac82b

File tree

10 files changed

+68
-64
lines changed

10 files changed

+68
-64
lines changed

stdlib/src/array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,10 +1324,10 @@ mod array {
13241324
}
13251325
}
13261326

1327-
impl<'a> BufferResizeGuard<'a> for PyArray {
1328-
type Resizable = PyRwLockWriteGuard<'a, ArrayContentType>;
1327+
impl BufferResizeGuard for PyArray {
1328+
type Resizable<'a> = PyRwLockWriteGuard<'a, ArrayContentType>;
13291329

1330-
fn try_resizable_opt(&'a self) -> Option<Self::Resizable> {
1330+
fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>> {
13311331
let w = self.write();
13321332
(self.exports.load(atomic::Ordering::SeqCst) == 0).then_some(w)
13331333
}

vm/src/anystr.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ use crate::{
77
use num_traits::{cast::ToPrimitive, sign::Signed};
88

99
#[derive(FromArgs)]
10-
pub struct SplitArgs<'s, T: TryFromObject + AnyStrWrapper<'s>> {
10+
pub struct SplitArgs<T: TryFromObject + AnyStrWrapper> {
1111
#[pyarg(any, default)]
1212
sep: Option<T>,
1313
#[pyarg(any, default = "-1")]
1414
maxsplit: isize,
15-
_phantom: std::marker::PhantomData<&'s ()>,
1615
}
1716

18-
impl<'s, T: TryFromObject + AnyStrWrapper<'s>> SplitArgs<'s, T> {
17+
impl<T: TryFromObject + AnyStrWrapper> SplitArgs<T> {
1918
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<(Option<T>, isize)> {
2019
let sep = if let Some(s) = self.sep {
2120
let sep = s.as_ref();
@@ -69,10 +68,10 @@ impl StartsEndsWithArgs {
6968
}
7069

7170
#[inline]
72-
pub fn prepare<'s, S, F>(self, s: &'s S, len: usize, substr: F) -> Option<(PyObjectRef, &'s S)>
71+
pub fn prepare<S, F>(self, s: &S, len: usize, substr: F) -> Option<(PyObjectRef, &S)>
7372
where
74-
S: ?Sized + AnyStr<'s>,
75-
F: Fn(&'s S, std::ops::Range<usize>) -> &'s S,
73+
S: ?Sized + AnyStr,
74+
F: Fn(&S, std::ops::Range<usize>) -> &S,
7675
{
7776
let (affix, range) = self.get_value(len);
7877
let substr = if let Some(range) = range {
@@ -133,8 +132,8 @@ impl StringRange for std::ops::Range<usize> {
133132
}
134133
}
135134

136-
pub trait AnyStrWrapper<'s> {
137-
type Str: ?Sized + AnyStr<'s>;
135+
pub trait AnyStrWrapper {
136+
type Str: ?Sized + AnyStr;
138137
fn as_ref(&self) -> &Self::Str;
139138
}
140139

@@ -147,20 +146,23 @@ where
147146
fn push_str(&mut self, s: &S);
148147
}
149148

150-
// TODO: GATs for `'s` once stabilized
151-
pub trait AnyStr<'s>: 's {
149+
pub trait AnyStr {
152150
type Char: Copy;
153151
type Container: AnyStrContainer<Self> + Extend<Self::Char>;
154-
type CharIter: Iterator<Item = char> + 's;
155-
type ElementIter: Iterator<Item = Self::Char> + 's;
152+
type CharIter<'a>: Iterator<Item = char> + 'a
153+
where
154+
Self: 'a;
155+
type ElementIter<'a>: Iterator<Item = Self::Char> + 'a
156+
where
157+
Self: 'a;
156158

157159
fn element_bytes_len(c: Self::Char) -> usize;
158160

159161
fn to_container(&self) -> Self::Container;
160162
fn as_bytes(&self) -> &[u8];
161163
fn as_utf8_str(&self) -> Result<&str, std::str::Utf8Error>;
162-
fn chars(&'s self) -> Self::CharIter;
163-
fn elements(&'s self) -> Self::ElementIter;
164+
fn chars(&self) -> Self::CharIter<'_>;
165+
fn elements(&self) -> Self::ElementIter<'_>;
164166
fn get_bytes(&self, range: std::ops::Range<usize>) -> &Self;
165167
// FIXME: get_chars is expensive for str
166168
fn get_chars(&self, range: std::ops::Range<usize>) -> &Self;
@@ -179,14 +181,14 @@ pub trait AnyStr<'s>: 's {
179181

180182
fn py_split<T, SP, SN, SW, R>(
181183
&self,
182-
args: SplitArgs<'s, T>,
184+
args: SplitArgs<T>,
183185
vm: &VirtualMachine,
184186
split: SP,
185187
splitn: SN,
186188
splitw: SW,
187189
) -> PyResult<Vec<R>>
188190
where
189-
T: TryFromObject + AnyStrWrapper<'s, Str = Self>,
191+
T: TryFromObject + AnyStrWrapper<Str = Self>,
190192
SP: Fn(&Self, &Self, &VirtualMachine) -> Vec<R>,
191193
SN: Fn(&Self, &Self, usize, &VirtualMachine) -> Vec<R>,
192194
SW: Fn(&Self, isize, &VirtualMachine) -> Vec<R>,
@@ -247,7 +249,7 @@ pub trait AnyStr<'s>: 's {
247249
func_default: FD,
248250
) -> &'a Self
249251
where
250-
S: AnyStrWrapper<'s, Str = Self>,
252+
S: AnyStrWrapper<Str = Self>,
251253
FC: Fn(&'a Self, &Self) -> &'a Self,
252254
FD: Fn(&'a Self) -> &'a Self,
253255
{
@@ -308,10 +310,10 @@ pub trait AnyStr<'s>: 's {
308310
self.py_pad(width - len, 0, fillchar)
309311
}
310312

311-
fn py_join<'a>(
313+
fn py_join(
312314
&self,
313315
mut iter: impl std::iter::Iterator<
314-
Item = PyResult<impl AnyStrWrapper<'s, Str = Self> + TryFromObject>,
316+
Item = PyResult<impl AnyStrWrapper<Str = Self> + TryFromObject>,
315317
>,
316318
) -> PyResult<Self::Container> {
317319
let mut joined = if let Some(elem) = iter.next() {
@@ -413,7 +415,7 @@ pub trait AnyStr<'s>: 's {
413415
rustpython_common::str::zfill(self.as_bytes(), width)
414416
}
415417

416-
fn py_iscase<F, G>(&'s self, is_case: F, is_opposite: G) -> bool
418+
fn py_iscase<F, G>(&self, is_case: F, is_opposite: G) -> bool
417419
where
418420
F: Fn(char) -> bool,
419421
G: Fn(char) -> bool,

vm/src/builtins/bytearray.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,10 @@ impl AsBuffer for PyByteArray {
782782
}
783783
}
784784

785-
impl<'a> BufferResizeGuard<'a> for PyByteArray {
786-
type Resizable = PyRwLockWriteGuard<'a, PyBytesInner>;
785+
impl BufferResizeGuard for PyByteArray {
786+
type Resizable<'a> = PyRwLockWriteGuard<'a, PyBytesInner>;
787787

788-
fn try_resizable_opt(&'a self) -> Option<Self::Resizable> {
788+
fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>> {
789789
let w = self.inner.write();
790790
(self.exports.load(Ordering::SeqCst) == 0).then_some(w)
791791
}

vm/src/builtins/list.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,14 @@ where
365365
}
366366
}
367367

368-
impl<'a> MutObjectSequenceOp<'a> for PyList {
369-
type Guard = PyMappedRwLockReadGuard<'a, [PyObjectRef]>;
368+
impl MutObjectSequenceOp for PyList {
369+
type Guard<'a> = PyMappedRwLockReadGuard<'a, [PyObjectRef]>;
370370

371-
fn do_get(index: usize, guard: &Self::Guard) -> Option<&PyObjectRef> {
371+
fn do_get<'a>(index: usize, guard: &'a Self::Guard<'_>) -> Option<&'a PyObjectRef> {
372372
guard.get(index)
373373
}
374374

375-
fn do_lock(&'a self) -> Self::Guard {
375+
fn do_lock(&self) -> Self::Guard<'_> {
376376
self.borrow_vec()
377377
}
378378
}

vm/src/builtins/str.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ impl ToPyObject for AsciiString {
13691369
}
13701370
}
13711371

1372-
type SplitArgs<'a> = anystr::SplitArgs<'a, PyStrRef>;
1372+
type SplitArgs = anystr::SplitArgs<PyStrRef>;
13731373

13741374
#[derive(FromArgs)]
13751375
pub struct FindArgs {
@@ -1582,7 +1582,7 @@ mod tests {
15821582
}
15831583
}
15841584

1585-
impl<'s> AnyStrWrapper<'s> for PyStrRef {
1585+
impl AnyStrWrapper for PyStrRef {
15861586
type Str = str;
15871587
fn as_ref(&self) -> &str {
15881588
self.as_str()
@@ -1603,11 +1603,11 @@ impl AnyStrContainer<str> for String {
16031603
}
16041604
}
16051605

1606-
impl<'s> AnyStr<'s> for str {
1606+
impl AnyStr for str {
16071607
type Char = char;
16081608
type Container = String;
1609-
type CharIter = std::str::Chars<'s>;
1610-
type ElementIter = std::str::Chars<'s>;
1609+
type CharIter<'a> = std::str::Chars<'a>;
1610+
type ElementIter<'a> = std::str::Chars<'a>;
16111611

16121612
fn element_bytes_len(c: char) -> usize {
16131613
c.len_utf8()
@@ -1625,11 +1625,11 @@ impl<'s> AnyStr<'s> for str {
16251625
Ok(self)
16261626
}
16271627

1628-
fn chars(&'s self) -> Self::CharIter {
1628+
fn chars(&self) -> Self::CharIter<'_> {
16291629
str::chars(self)
16301630
}
16311631

1632-
fn elements(&'s self) -> Self::ElementIter {
1632+
fn elements(&self) -> Self::ElementIter<'_> {
16331633
str::chars(self)
16341634
}
16351635

vm/src/bytesinner.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl ByteInnerTranslateOptions {
239239
}
240240
}
241241

242-
pub type ByteInnerSplitOptions<'a> = anystr::SplitArgs<'a, PyBytesInner>;
242+
pub type ByteInnerSplitOptions = anystr::SplitArgs<PyBytesInner>;
243243

244244
impl PyBytesInner {
245245
#[inline]
@@ -971,7 +971,7 @@ pub trait ByteOr: ToPrimitive {
971971

972972
impl ByteOr for BigInt {}
973973

974-
impl<'s> AnyStrWrapper<'s> for PyBytesInner {
974+
impl AnyStrWrapper for PyBytesInner {
975975
type Str = [u8];
976976
fn as_ref(&self) -> &[u8] {
977977
&self.elements
@@ -994,11 +994,11 @@ impl AnyStrContainer<[u8]> for Vec<u8> {
994994

995995
const ASCII_WHITESPACES: [u8; 6] = [0x20, 0x09, 0x0a, 0x0c, 0x0d, 0x0b];
996996

997-
impl<'s> AnyStr<'s> for [u8] {
997+
impl AnyStr for [u8] {
998998
type Char = u8;
999999
type Container = Vec<u8>;
1000-
type CharIter = bstr::Chars<'s>;
1001-
type ElementIter = std::iter::Copied<std::slice::Iter<'s, u8>>;
1000+
type CharIter<'a> = bstr::Chars<'a>;
1001+
type ElementIter<'a> = std::iter::Copied<std::slice::Iter<'a, u8>>;
10021002

10031003
fn element_bytes_len(_: u8) -> usize {
10041004
1
@@ -1016,11 +1016,11 @@ impl<'s> AnyStr<'s> for [u8] {
10161016
std::str::from_utf8(self)
10171017
}
10181018

1019-
fn chars(&'s self) -> Self::CharIter {
1019+
fn chars(&self) -> Self::CharIter<'_> {
10201020
bstr::ByteSlice::chars(self)
10211021
}
10221022

1023-
fn elements(&'s self) -> Self::ElementIter {
1023+
fn elements(&self) -> Self::ElementIter<'_> {
10241024
self.iter().copied()
10251025
}
10261026

vm/src/protocol/buffer.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,12 @@ impl BufferDescriptor {
384384
// TODO: support fortain order
385385
}
386386

387-
pub trait BufferResizeGuard<'a> {
388-
type Resizable: 'a;
389-
fn try_resizable_opt(&'a self) -> Option<Self::Resizable>;
390-
fn try_resizable(&'a self, vm: &VirtualMachine) -> PyResult<Self::Resizable> {
387+
pub trait BufferResizeGuard {
388+
type Resizable<'a>: 'a
389+
where
390+
Self: 'a;
391+
fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>>;
392+
fn try_resizable(&self, vm: &VirtualMachine) -> PyResult<Self::Resizable<'_>> {
391393
self.try_resizable_opt().ok_or_else(|| {
392394
vm.new_buffer_error("Existing exports of data: object cannot be re-sized".to_owned())
393395
})

vm/src/sequence.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use crate::{
55
use optional::Optioned;
66
use std::ops::Range;
77

8-
pub trait MutObjectSequenceOp<'a> {
9-
type Guard;
8+
pub trait MutObjectSequenceOp {
9+
type Guard<'a>: 'a;
1010

11-
fn do_get(index: usize, guard: &Self::Guard) -> Option<&PyObjectRef>;
12-
fn do_lock(&'a self) -> Self::Guard;
11+
fn do_get<'a>(index: usize, guard: &'a Self::Guard<'_>) -> Option<&'a PyObjectRef>;
12+
fn do_lock(&self) -> Self::Guard<'_>;
1313

14-
fn mut_count(&'a self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<usize> {
14+
fn mut_count(&self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<usize> {
1515
let mut count = 0;
1616
self._mut_iter_equal_skeleton::<_, false>(vm, needle, 0..isize::MAX as usize, || {
1717
count += 1
@@ -20,24 +20,24 @@ pub trait MutObjectSequenceOp<'a> {
2020
}
2121

2222
fn mut_index_range(
23-
&'a self,
23+
&self,
2424
vm: &VirtualMachine,
2525
needle: &PyObject,
2626
range: Range<usize>,
2727
) -> PyResult<Optioned<usize>> {
2828
self._mut_iter_equal_skeleton::<_, true>(vm, needle, range, || {})
2929
}
3030

31-
fn mut_index(&'a self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<Optioned<usize>> {
31+
fn mut_index(&self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<Optioned<usize>> {
3232
self.mut_index_range(vm, needle, 0..isize::MAX as usize)
3333
}
3434

35-
fn mut_contains(&'a self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<bool> {
35+
fn mut_contains(&self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<bool> {
3636
self.mut_index(vm, needle).map(|x| x.is_some())
3737
}
3838

3939
fn _mut_iter_equal_skeleton<F, const SHORT: bool>(
40-
&'a self,
40+
&self,
4141
vm: &VirtualMachine,
4242
needle: &PyObject,
4343
range: Range<usize>,

vm/src/stdlib/collections.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,14 @@ mod _collections {
429429
}
430430
}
431431

432-
impl<'a> MutObjectSequenceOp<'a> for PyDeque {
433-
type Guard = PyRwLockReadGuard<'a, VecDeque<PyObjectRef>>;
432+
impl MutObjectSequenceOp for PyDeque {
433+
type Guard<'a> = PyRwLockReadGuard<'a, VecDeque<PyObjectRef>>;
434434

435-
fn do_get(index: usize, guard: &Self::Guard) -> Option<&PyObjectRef> {
435+
fn do_get<'a>(index: usize, guard: &'a Self::Guard<'_>) -> Option<&'a PyObjectRef> {
436436
guard.get(index)
437437
}
438438

439-
fn do_lock(&'a self) -> Self::Guard {
439+
fn do_lock(&self) -> Self::Guard<'_> {
440440
self.borrow_deque()
441441
}
442442
}

vm/src/stdlib/io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,10 +3389,10 @@ mod _io {
33893389
},
33903390
};
33913391

3392-
impl<'a> BufferResizeGuard<'a> for BytesIO {
3393-
type Resizable = PyRwLockWriteGuard<'a, BufferedIO>;
3392+
impl BufferResizeGuard for BytesIO {
3393+
type Resizable<'a> = PyRwLockWriteGuard<'a, BufferedIO>;
33943394

3395-
fn try_resizable_opt(&'a self) -> Option<Self::Resizable> {
3395+
fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>> {
33963396
let w = self.buffer.write();
33973397
(self.exports.load() == 0).then_some(w)
33983398
}

0 commit comments

Comments
 (0)