forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyteslike.rs
More file actions
95 lines (84 loc) · 2.86 KB
/
byteslike.rs
File metadata and controls
95 lines (84 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::obj::objbytearray::{PyByteArray, PyByteArrayRef};
use crate::obj::objbytes::{PyBytes, PyBytesRef};
use crate::pyobject::PyObjectRef;
use crate::pyobject::{BorrowValue, PyResult, TryFromObject, TypeProtocol};
use crate::stdlib::array::{PyArray, PyArrayRef};
use crate::vm::VirtualMachine;
#[derive(Debug)]
pub enum PyBytesLike {
Bytes(PyBytesRef),
Bytearray(PyByteArrayRef),
Array(PyArrayRef),
}
impl TryFromObject for PyBytesLike {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
match_class!(match obj {
b @ PyBytes => Ok(PyBytesLike::Bytes(b)),
b @ PyByteArray => Ok(PyBytesLike::Bytearray(b)),
array @ PyArray => Ok(PyBytesLike::Array(array)),
obj => Err(vm.new_type_error(format!(
"a bytes-like object is required, not {}",
obj.class()
))),
})
}
}
impl PyBytesLike {
pub fn len(&self) -> usize {
match self {
PyBytesLike::Bytes(b) => b.len(),
PyBytesLike::Bytearray(b) => b.borrow_value().len(),
PyBytesLike::Array(array) => array.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn to_cow(&self) -> std::borrow::Cow<[u8]> {
match self {
PyBytesLike::Bytes(b) => b.borrow_value().into(),
PyBytesLike::Bytearray(b) => b.borrow_value().elements.clone().into(),
PyBytesLike::Array(array) => array.tobytes().into(),
}
}
#[inline]
pub fn with_ref<R>(&self, f: impl FnOnce(&[u8]) -> R) -> R {
match self {
PyBytesLike::Bytes(b) => f(b.borrow_value()),
PyBytesLike::Bytearray(b) => f(&b.borrow_value().elements),
PyBytesLike::Array(array) => f(&*array.get_bytes()),
}
}
}
pub enum PyRwBytesLike {
Bytearray(PyByteArrayRef),
Array(PyArrayRef),
}
impl TryFromObject for PyRwBytesLike {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
match_class!(match obj {
b @ PyByteArray => Ok(PyRwBytesLike::Bytearray(b)),
array @ PyArray => Ok(PyRwBytesLike::Array(array)),
obj =>
Err(vm.new_type_error(format!("a buffer object is required, not {}", obj.class()))),
})
}
}
impl PyRwBytesLike {
pub fn len(&self) -> usize {
match self {
PyRwBytesLike::Bytearray(b) => b.borrow_value().len(),
PyRwBytesLike::Array(array) => array.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn with_ref<R>(&self, f: impl FnOnce(&mut [u8]) -> R) -> R {
match self {
PyRwBytesLike::Bytearray(b) => f(&mut b.borrow_value_mut().elements),
PyRwBytesLike::Array(array) => f(&mut array.get_bytes_mut()),
}
}
}