Skip to content

Commit 60bbaf1

Browse files
committed
rustpython_vm::protocol to hold protocols
1 parent f0dd29d commit 60bbaf1

File tree

13 files changed

+26
-23
lines changed

13 files changed

+26
-23
lines changed

vm/src/builtins/bytearray.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use super::pystr::PyStrRef;
66
use super::pytype::PyTypeRef;
77
use super::tuple::PyTupleRef;
88
use crate::anystr::{self, AnyStr};
9-
use crate::buffer::{BufferOptions, PyBuffer, PyBufferInternal, ResizeGuard};
109
use crate::bytesinner::{
1110
bytes_decode, bytes_from_object, value_from_object, ByteInnerFindOptions, ByteInnerNewOptions,
1211
ByteInnerPaddingOptions, ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs,
@@ -19,6 +18,7 @@ use crate::common::lock::{
1918
PyRwLockWriteGuard,
2019
};
2120
use crate::function::{ArgIterable, FuncArgs, OptionalArg, OptionalOption};
21+
use crate::protocol::{BufferInternal, BufferOptions, PyBuffer, ResizeGuard};
2222
use crate::sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex};
2323
use crate::slots::{
2424
AsBuffer, Callable, Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhashable,
@@ -684,7 +684,7 @@ impl AsBuffer for PyByteArray {
684684
}
685685
}
686686

687-
impl PyBufferInternal for PyRef<PyByteArray> {
687+
impl BufferInternal for PyRef<PyByteArray> {
688688
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
689689
self.borrow_buf().into()
690690
}

vm/src/builtins/bytes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use super::int::PyIntRef;
33
use super::pystr::PyStrRef;
44
use super::pytype::PyTypeRef;
55
use crate::anystr::{self, AnyStr};
6-
use crate::buffer::{BufferOptions, PyBuffer, PyBufferInternal};
76
use crate::builtins::tuple::PyTupleRef;
87
use crate::bytesinner::{
98
bytes_decode, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
@@ -12,6 +11,7 @@ use crate::bytesinner::{
1211
use crate::byteslike::ArgBytesLike;
1312
use crate::common::hash::PyHash;
1413
use crate::function::{ArgIterable, OptionalArg, OptionalOption};
14+
use crate::protocol::{BufferInternal, BufferOptions, PyBuffer};
1515
use crate::slots::{
1616
AsBuffer, Callable, Comparable, Hashable, Iterable, PyComparisonOp, PyIter, SlotConstructor,
1717
};
@@ -532,7 +532,7 @@ impl AsBuffer for PyBytes {
532532
}
533533
}
534534

535-
impl PyBufferInternal for PyRef<PyBytes> {
535+
impl BufferInternal for PyRef<PyBytes> {
536536
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
537537
self.as_bytes().into()
538538
}

vm/src/builtins/memory.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::buffer::{BufferOptions, PyBuffer, PyBufferInternal};
21
use crate::builtins::slice::PySliceRef;
32
use crate::builtins::{PyBytes, PyBytesRef, PyList, PyListRef, PyStr, PyStrRef, PyTypeRef};
43
use crate::bytesinner::bytes_to_hex;
@@ -9,6 +8,7 @@ use crate::common::{
98
rc::PyRc,
109
};
1110
use crate::function::{FuncArgs, OptionalArg};
11+
use crate::protocol::{BufferInternal, BufferOptions, PyBuffer};
1212
use crate::sliceable::{convert_slice, wrap_index, SequenceIndex};
1313
use crate::slots::{AsBuffer, Comparable, Hashable, PyComparisonOp, SlotConstructor};
1414
use crate::stdlib::pystruct::_struct::FormatSpec;
@@ -692,7 +692,7 @@ impl AsBuffer for PyMemoryView {
692692

693693
#[derive(Debug)]
694694
struct Released;
695-
impl PyBufferInternal for Released {
695+
impl BufferInternal for Released {
696696
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
697697
panic!();
698698
}
@@ -706,7 +706,7 @@ impl PyBufferInternal for Released {
706706
fn retain(&self) {}
707707
}
708708

709-
impl PyBufferInternal for PyMemoryView {
709+
impl BufferInternal for PyMemoryView {
710710
// NOTE: This impl maybe is anti-pattern. Only used for internal usage.
711711
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
712712
BorrowedValue::map(self.buffer.internal.obj_bytes(), |x| {
@@ -725,7 +725,7 @@ impl PyBufferInternal for PyMemoryView {
725725
fn retain(&self) {}
726726
}
727727

728-
impl PyBufferInternal for PyRef<PyMemoryView> {
728+
impl BufferInternal for PyRef<PyMemoryView> {
729729
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
730730
self.deref().obj_bytes()
731731
}

vm/src/byteslike.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::buffer::PyBuffer;
21
use crate::builtins::PyStrRef;
32
use crate::common::borrow::{BorrowedValue, BorrowedValueMut};
3+
use crate::protocol::PyBuffer;
44
use crate::vm::VirtualMachine;
55
use crate::{PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject};
66

vm/src/cformat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Implementation of Printf-Style string formatting
22
//! [https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting]
33
4-
use crate::buffer::PyBuffer;
54
use crate::builtins::{int, try_f64_to_bigint, tuple, IntoPyFloat, PyBytes, PyFloat, PyInt, PyStr};
65
use crate::common::float_ops;
6+
use crate::protocol::PyBuffer;
77
use crate::{
88
ItemProtocol, PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject, TypeProtocol,
99
VirtualMachine,

vm/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub use rustpython_derive::*;
4343
pub mod macros;
4444

4545
mod anystr;
46-
mod buffer;
4746
pub mod builtins;
4847
mod bytesinner;
4948
pub mod byteslike;
@@ -62,6 +61,7 @@ mod frozen;
6261
pub mod function;
6362
pub mod import;
6463
pub mod iterator;
64+
mod protocol;
6565
pub mod py_io;
6666
pub mod py_serde;
6767
mod pyobject;

vm/src/buffer.rs renamed to vm/src/protocol/buffer.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::PyThreadingConstraint;
66
use crate::{PyObjectRef, PyResult, TryFromBorrowedObject, TypeProtocol, VirtualMachine};
77
use std::{borrow::Cow, fmt::Debug};
88

9-
pub trait PyBufferInternal: Debug + PyThreadingConstraint {
9+
pub trait BufferInternal: Debug + PyThreadingConstraint {
1010
/// Get the full inner buffer of this memory. You probably want [`as_contiguous()`], as
1111
/// `obj_bytes` doesn't take into account the range a memoryview might operate on, among other
1212
/// footguns.
@@ -24,13 +24,13 @@ pub trait PyBufferInternal: Debug + PyThreadingConstraint {
2424
pub struct PyBuffer {
2525
pub obj: PyObjectRef,
2626
pub options: BufferOptions,
27-
pub(crate) internal: PyRc<dyn PyBufferInternal>,
27+
pub(crate) internal: PyRc<dyn BufferInternal>,
2828
}
2929

3030
impl PyBuffer {
3131
pub fn new(
3232
obj: PyObjectRef,
33-
buffer: impl PyBufferInternal + 'static,
33+
buffer: impl BufferInternal + 'static,
3434
options: BufferOptions,
3535
) -> Self {
3636
buffer.retain();
@@ -119,7 +119,7 @@ impl TryFromBorrowedObject for PyBuffer {
119119
}
120120

121121
// What we actually want to implement is:
122-
// impl<T> Drop for T where T: PyBufferInternal
122+
// impl<T> Drop for T where T: BufferInternal
123123
// but it is not supported by Rust
124124
impl Drop for PyBuffer {
125125
fn drop(&mut self) {

vm/src/protocol/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod buffer;
2+
3+
pub(crate) use buffer::{BufferInternal, BufferOptions, PyBuffer, ResizeGuard};

vm/src/slots.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::buffer::PyBuffer;
21
use crate::builtins::{PyStrRef, PyTypeRef};
32
use crate::common::hash::PyHash;
43
use crate::common::lock::PyRwLock;
54
use crate::function::{FromArgs, FuncArgs, OptionalArg};
5+
use crate::protocol::PyBuffer;
66
use crate::utils::Either;
77
use crate::VirtualMachine;
88
use crate::{

vm/src/stdlib/array.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ mod array {
1111
str::wchar_t,
1212
};
1313
use crate::{
14-
buffer::{BufferOptions, PyBuffer, PyBufferInternal, ResizeGuard},
1514
builtins::{
1615
IntoPyFloat, PyByteArray, PyBytes, PyBytesRef, PyIntRef, PyList, PyListRef, PySliceRef,
1716
PyStr, PyStrRef, PyTypeRef,
1817
},
1918
byteslike::ArgBytesLike,
2019
function::{ArgIterable, OptionalArg},
20+
protocol::{BufferInternal, BufferOptions, PyBuffer, ResizeGuard},
2121
sliceable::{saturate_index, PySliceableSequence, PySliceableSequenceMut, SequenceIndex},
2222
slots::{AsBuffer, Comparable, Iterable, PyComparisonOp, PyIter, SlotConstructor},
2323
IdProtocol, IntoPyObject, IntoPyResult, PyComparisonValue, PyObjectRef, PyRef, PyResult,
@@ -1137,7 +1137,7 @@ mod array {
11371137
}
11381138
}
11391139

1140-
impl PyBufferInternal for PyRef<PyArray> {
1140+
impl BufferInternal for PyRef<PyArray> {
11411141
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
11421142
self.get_bytes().into()
11431143
}

vm/src/stdlib/io.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ mod _io {
7474
use std::io::{self, prelude::*, Cursor, SeekFrom};
7575
use std::ops::Range;
7676

77-
use crate::buffer::{BufferOptions, PyBuffer, PyBufferInternal, ResizeGuard};
7877
use crate::builtins::memory::PyMemoryView;
7978
use crate::builtins::{
8079
bytes::{PyBytes, PyBytesRef},
@@ -89,6 +88,7 @@ mod _io {
8988
use crate::common::rc::PyRc;
9089
use crate::exceptions::{self, PyBaseExceptionRef};
9190
use crate::function::{ArgIterable, FuncArgs, OptionalArg, OptionalOption};
91+
use crate::protocol::{BufferInternal, BufferOptions, PyBuffer, ResizeGuard};
9292
use crate::slots::SlotConstructor;
9393
use crate::utils::Either;
9494
use crate::vm::{ReprGuard, VirtualMachine};
@@ -1318,7 +1318,7 @@ mod _io {
13181318
data: PyMutex<Vec<u8>>,
13191319
range: Range<usize>,
13201320
}
1321-
impl PyBufferInternal for BufferedRawBuffer {
1321+
impl BufferInternal for BufferedRawBuffer {
13221322
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
13231323
BorrowedValue::map(self.data.lock().into(), |data| &data[self.range.clone()])
13241324
}
@@ -3319,7 +3319,7 @@ mod _io {
33193319
}
33203320
}
33213321

3322-
impl PyBufferInternal for PyRef<BytesIO> {
3322+
impl BufferInternal for PyRef<BytesIO> {
33233323
fn obj_bytes(&self) -> BorrowedValue<[u8]> {
33243324
PyRwLockReadGuard::map(self.buffer.read(), |x| x.cursor.get_ref().as_slice()).into()
33253325
}

vm/src/stdlib/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::errno::errors;
22
use crate::crt_fd::Fd;
33
use crate::{
4-
buffer::PyBuffer,
54
builtins::{int, PyBytes, PyBytesRef, PySet, PyStr, PyStrRef},
65
exceptions::{IntoPyException, PyBaseExceptionRef},
76
function::{ArgumentError, FromArgs, FuncArgs},
7+
protocol::PyBuffer,
88
IntoPyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject,
99
TypeProtocol, VirtualMachine,
1010
};

vm/src/stdlib/sre.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ pub(crate) use _sre::make_module;
33
#[pymodule]
44
mod _sre {
55
use crate::{
6-
buffer::PyBuffer,
76
builtins::{
87
PyCallableIterator, PyDictRef, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef,
98
},
109
common::hash::PyHash,
1110
function::{ArgCallable, OptionalArg, PosArgs},
11+
protocol::PyBuffer,
1212
slots::{Comparable, Hashable},
1313
IntoPyObject, ItemProtocol, PyComparisonValue, PyObjectRef, PyRef, PyResult, PyValue,
1414
TryFromBorrowedObject, TryFromObject, VirtualMachine,

0 commit comments

Comments
 (0)