Merge pull request #4605 from coolreader18/no-hacky-gats

Switch hacky GAT-workarounds to use actual GATs
This commit is contained in:
Jeong YunWon
2023-03-02 12:58:26 +09:00
committed by GitHub
10 changed files with 68 additions and 64 deletions

View File

@@ -1324,10 +1324,10 @@ mod array {
}
}
impl<'a> BufferResizeGuard<'a> for PyArray {
type Resizable = PyRwLockWriteGuard<'a, ArrayContentType>;
impl BufferResizeGuard for PyArray {
type Resizable<'a> = PyRwLockWriteGuard<'a, ArrayContentType>;
fn try_resizable_opt(&'a self) -> Option<Self::Resizable> {
fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>> {
let w = self.write();
(self.exports.load(atomic::Ordering::SeqCst) == 0).then_some(w)
}

View File

@@ -7,15 +7,14 @@ use crate::{
use num_traits::{cast::ToPrimitive, sign::Signed};
#[derive(FromArgs)]
pub struct SplitArgs<'s, T: TryFromObject + AnyStrWrapper<'s>> {
pub struct SplitArgs<T: TryFromObject + AnyStrWrapper> {
#[pyarg(any, default)]
sep: Option<T>,
#[pyarg(any, default = "-1")]
maxsplit: isize,
_phantom: std::marker::PhantomData<&'s ()>,
}
impl<'s, T: TryFromObject + AnyStrWrapper<'s>> SplitArgs<'s, T> {
impl<T: TryFromObject + AnyStrWrapper> SplitArgs<T> {
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<(Option<T>, isize)> {
let sep = if let Some(s) = self.sep {
let sep = s.as_ref();
@@ -69,10 +68,10 @@ impl StartsEndsWithArgs {
}
#[inline]
pub fn prepare<'s, S, F>(self, s: &'s S, len: usize, substr: F) -> Option<(PyObjectRef, &'s S)>
pub fn prepare<S, F>(self, s: &S, len: usize, substr: F) -> Option<(PyObjectRef, &S)>
where
S: ?Sized + AnyStr<'s>,
F: Fn(&'s S, std::ops::Range<usize>) -> &'s S,
S: ?Sized + AnyStr,
F: Fn(&S, std::ops::Range<usize>) -> &S,
{
let (affix, range) = self.get_value(len);
let substr = if let Some(range) = range {
@@ -133,8 +132,8 @@ impl StringRange for std::ops::Range<usize> {
}
}
pub trait AnyStrWrapper<'s> {
type Str: ?Sized + AnyStr<'s>;
pub trait AnyStrWrapper {
type Str: ?Sized + AnyStr;
fn as_ref(&self) -> &Self::Str;
}
@@ -147,20 +146,23 @@ where
fn push_str(&mut self, s: &S);
}
// TODO: GATs for `'s` once stabilized
pub trait AnyStr<'s>: 's {
pub trait AnyStr {
type Char: Copy;
type Container: AnyStrContainer<Self> + Extend<Self::Char>;
type CharIter: Iterator<Item = char> + 's;
type ElementIter: Iterator<Item = Self::Char> + 's;
type CharIter<'a>: Iterator<Item = char> + 'a
where
Self: 'a;
type ElementIter<'a>: Iterator<Item = Self::Char> + 'a
where
Self: 'a;
fn element_bytes_len(c: Self::Char) -> usize;
fn to_container(&self) -> Self::Container;
fn as_bytes(&self) -> &[u8];
fn as_utf8_str(&self) -> Result<&str, std::str::Utf8Error>;
fn chars(&'s self) -> Self::CharIter;
fn elements(&'s self) -> Self::ElementIter;
fn chars(&self) -> Self::CharIter<'_>;
fn elements(&self) -> Self::ElementIter<'_>;
fn get_bytes(&self, range: std::ops::Range<usize>) -> &Self;
// FIXME: get_chars is expensive for str
fn get_chars(&self, range: std::ops::Range<usize>) -> &Self;
@@ -179,14 +181,14 @@ pub trait AnyStr<'s>: 's {
fn py_split<T, SP, SN, SW, R>(
&self,
args: SplitArgs<'s, T>,
args: SplitArgs<T>,
vm: &VirtualMachine,
split: SP,
splitn: SN,
splitw: SW,
) -> PyResult<Vec<R>>
where
T: TryFromObject + AnyStrWrapper<'s, Str = Self>,
T: TryFromObject + AnyStrWrapper<Str = Self>,
SP: Fn(&Self, &Self, &VirtualMachine) -> Vec<R>,
SN: Fn(&Self, &Self, usize, &VirtualMachine) -> Vec<R>,
SW: Fn(&Self, isize, &VirtualMachine) -> Vec<R>,
@@ -247,7 +249,7 @@ pub trait AnyStr<'s>: 's {
func_default: FD,
) -> &'a Self
where
S: AnyStrWrapper<'s, Str = Self>,
S: AnyStrWrapper<Str = Self>,
FC: Fn(&'a Self, &Self) -> &'a Self,
FD: Fn(&'a Self) -> &'a Self,
{
@@ -308,10 +310,10 @@ pub trait AnyStr<'s>: 's {
self.py_pad(width - len, 0, fillchar)
}
fn py_join<'a>(
fn py_join(
&self,
mut iter: impl std::iter::Iterator<
Item = PyResult<impl AnyStrWrapper<'s, Str = Self> + TryFromObject>,
Item = PyResult<impl AnyStrWrapper<Str = Self> + TryFromObject>,
>,
) -> PyResult<Self::Container> {
let mut joined = if let Some(elem) = iter.next() {
@@ -413,7 +415,7 @@ pub trait AnyStr<'s>: 's {
rustpython_common::str::zfill(self.as_bytes(), width)
}
fn py_iscase<F, G>(&'s self, is_case: F, is_opposite: G) -> bool
fn py_iscase<F, G>(&self, is_case: F, is_opposite: G) -> bool
where
F: Fn(char) -> bool,
G: Fn(char) -> bool,

View File

@@ -782,10 +782,10 @@ impl AsBuffer for PyByteArray {
}
}
impl<'a> BufferResizeGuard<'a> for PyByteArray {
type Resizable = PyRwLockWriteGuard<'a, PyBytesInner>;
impl BufferResizeGuard for PyByteArray {
type Resizable<'a> = PyRwLockWriteGuard<'a, PyBytesInner>;
fn try_resizable_opt(&'a self) -> Option<Self::Resizable> {
fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>> {
let w = self.inner.write();
(self.exports.load(Ordering::SeqCst) == 0).then_some(w)
}

View File

@@ -365,14 +365,14 @@ where
}
}
impl<'a> MutObjectSequenceOp<'a> for PyList {
type Guard = PyMappedRwLockReadGuard<'a, [PyObjectRef]>;
impl MutObjectSequenceOp for PyList {
type Guard<'a> = PyMappedRwLockReadGuard<'a, [PyObjectRef]>;
fn do_get(index: usize, guard: &Self::Guard) -> Option<&PyObjectRef> {
fn do_get<'a>(index: usize, guard: &'a Self::Guard<'_>) -> Option<&'a PyObjectRef> {
guard.get(index)
}
fn do_lock(&'a self) -> Self::Guard {
fn do_lock(&self) -> Self::Guard<'_> {
self.borrow_vec()
}
}

View File

@@ -1369,7 +1369,7 @@ impl ToPyObject for AsciiString {
}
}
type SplitArgs<'a> = anystr::SplitArgs<'a, PyStrRef>;
type SplitArgs = anystr::SplitArgs<PyStrRef>;
#[derive(FromArgs)]
pub struct FindArgs {
@@ -1582,7 +1582,7 @@ mod tests {
}
}
impl<'s> AnyStrWrapper<'s> for PyStrRef {
impl AnyStrWrapper for PyStrRef {
type Str = str;
fn as_ref(&self) -> &str {
self.as_str()
@@ -1603,11 +1603,11 @@ impl AnyStrContainer<str> for String {
}
}
impl<'s> AnyStr<'s> for str {
impl AnyStr for str {
type Char = char;
type Container = String;
type CharIter = std::str::Chars<'s>;
type ElementIter = std::str::Chars<'s>;
type CharIter<'a> = std::str::Chars<'a>;
type ElementIter<'a> = std::str::Chars<'a>;
fn element_bytes_len(c: char) -> usize {
c.len_utf8()
@@ -1625,11 +1625,11 @@ impl<'s> AnyStr<'s> for str {
Ok(self)
}
fn chars(&'s self) -> Self::CharIter {
fn chars(&self) -> Self::CharIter<'_> {
str::chars(self)
}
fn elements(&'s self) -> Self::ElementIter {
fn elements(&self) -> Self::ElementIter<'_> {
str::chars(self)
}

View File

@@ -239,7 +239,7 @@ impl ByteInnerTranslateOptions {
}
}
pub type ByteInnerSplitOptions<'a> = anystr::SplitArgs<'a, PyBytesInner>;
pub type ByteInnerSplitOptions = anystr::SplitArgs<PyBytesInner>;
impl PyBytesInner {
#[inline]
@@ -971,7 +971,7 @@ pub trait ByteOr: ToPrimitive {
impl ByteOr for BigInt {}
impl<'s> AnyStrWrapper<'s> for PyBytesInner {
impl AnyStrWrapper for PyBytesInner {
type Str = [u8];
fn as_ref(&self) -> &[u8] {
&self.elements
@@ -994,11 +994,11 @@ impl AnyStrContainer<[u8]> for Vec<u8> {
const ASCII_WHITESPACES: [u8; 6] = [0x20, 0x09, 0x0a, 0x0c, 0x0d, 0x0b];
impl<'s> AnyStr<'s> for [u8] {
impl AnyStr for [u8] {
type Char = u8;
type Container = Vec<u8>;
type CharIter = bstr::Chars<'s>;
type ElementIter = std::iter::Copied<std::slice::Iter<'s, u8>>;
type CharIter<'a> = bstr::Chars<'a>;
type ElementIter<'a> = std::iter::Copied<std::slice::Iter<'a, u8>>;
fn element_bytes_len(_: u8) -> usize {
1
@@ -1016,11 +1016,11 @@ impl<'s> AnyStr<'s> for [u8] {
std::str::from_utf8(self)
}
fn chars(&'s self) -> Self::CharIter {
fn chars(&self) -> Self::CharIter<'_> {
bstr::ByteSlice::chars(self)
}
fn elements(&'s self) -> Self::ElementIter {
fn elements(&self) -> Self::ElementIter<'_> {
self.iter().copied()
}

View File

@@ -384,10 +384,12 @@ impl BufferDescriptor {
// TODO: support fortain order
}
pub trait BufferResizeGuard<'a> {
type Resizable: 'a;
fn try_resizable_opt(&'a self) -> Option<Self::Resizable>;
fn try_resizable(&'a self, vm: &VirtualMachine) -> PyResult<Self::Resizable> {
pub trait BufferResizeGuard {
type Resizable<'a>: 'a
where
Self: 'a;
fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>>;
fn try_resizable(&self, vm: &VirtualMachine) -> PyResult<Self::Resizable<'_>> {
self.try_resizable_opt().ok_or_else(|| {
vm.new_buffer_error("Existing exports of data: object cannot be re-sized".to_owned())
})

View File

@@ -5,13 +5,13 @@ use crate::{
use optional::Optioned;
use std::ops::Range;
pub trait MutObjectSequenceOp<'a> {
type Guard;
pub trait MutObjectSequenceOp {
type Guard<'a>: 'a;
fn do_get(index: usize, guard: &Self::Guard) -> Option<&PyObjectRef>;
fn do_lock(&'a self) -> Self::Guard;
fn do_get<'a>(index: usize, guard: &'a Self::Guard<'_>) -> Option<&'a PyObjectRef>;
fn do_lock(&self) -> Self::Guard<'_>;
fn mut_count(&'a self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<usize> {
fn mut_count(&self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<usize> {
let mut count = 0;
self._mut_iter_equal_skeleton::<_, false>(vm, needle, 0..isize::MAX as usize, || {
count += 1
@@ -20,7 +20,7 @@ pub trait MutObjectSequenceOp<'a> {
}
fn mut_index_range(
&'a self,
&self,
vm: &VirtualMachine,
needle: &PyObject,
range: Range<usize>,
@@ -28,16 +28,16 @@ pub trait MutObjectSequenceOp<'a> {
self._mut_iter_equal_skeleton::<_, true>(vm, needle, range, || {})
}
fn mut_index(&'a self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<Optioned<usize>> {
fn mut_index(&self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<Optioned<usize>> {
self.mut_index_range(vm, needle, 0..isize::MAX as usize)
}
fn mut_contains(&'a self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<bool> {
fn mut_contains(&self, vm: &VirtualMachine, needle: &PyObject) -> PyResult<bool> {
self.mut_index(vm, needle).map(|x| x.is_some())
}
fn _mut_iter_equal_skeleton<F, const SHORT: bool>(
&'a self,
&self,
vm: &VirtualMachine,
needle: &PyObject,
range: Range<usize>,

View File

@@ -429,14 +429,14 @@ mod _collections {
}
}
impl<'a> MutObjectSequenceOp<'a> for PyDeque {
type Guard = PyRwLockReadGuard<'a, VecDeque<PyObjectRef>>;
impl MutObjectSequenceOp for PyDeque {
type Guard<'a> = PyRwLockReadGuard<'a, VecDeque<PyObjectRef>>;
fn do_get(index: usize, guard: &Self::Guard) -> Option<&PyObjectRef> {
fn do_get<'a>(index: usize, guard: &'a Self::Guard<'_>) -> Option<&'a PyObjectRef> {
guard.get(index)
}
fn do_lock(&'a self) -> Self::Guard {
fn do_lock(&self) -> Self::Guard<'_> {
self.borrow_deque()
}
}

View File

@@ -3389,10 +3389,10 @@ mod _io {
},
};
impl<'a> BufferResizeGuard<'a> for BytesIO {
type Resizable = PyRwLockWriteGuard<'a, BufferedIO>;
impl BufferResizeGuard for BytesIO {
type Resizable<'a> = PyRwLockWriteGuard<'a, BufferedIO>;
fn try_resizable_opt(&'a self) -> Option<Self::Resizable> {
fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>> {
let w = self.buffer.write();
(self.exports.load() == 0).then_some(w)
}