Skip to content

Commit a1ca535

Browse files
committed
Make bufferoptions.format a Cow<'static, str>
1 parent 0745725 commit a1ca535

2 files changed

Lines changed: 44 additions & 29 deletions

File tree

vm/src/builtins/memory.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt::Debug, ops::Deref};
1+
use std::{borrow::Cow, fmt::Debug, ops::Deref};
22

33
use crate::builtins::bytes::{PyBytes, PyBytesRef};
44
use crate::builtins::list::{PyList, PyListRef};
@@ -37,6 +37,11 @@ impl Deref for BufferRef {
3737
self.0.deref()
3838
}
3939
}
40+
impl BufferRef {
41+
pub fn new(buffer: impl Buffer + 'static) -> Self {
42+
Self(Box::new(buffer))
43+
}
44+
}
4045
impl From<Box<dyn Buffer>> for BufferRef {
4146
fn from(buffer: Box<dyn Buffer>) -> Self {
4247
BufferRef(buffer)
@@ -76,33 +81,37 @@ pub struct BufferOptions {
7681
pub len: usize,
7782
pub itemsize: usize,
7883
pub contiguous: bool,
79-
pub format: String,
84+
pub format: Cow<'static, str>,
8085
// TODO: support multiple dimension array
8186
pub ndim: usize,
8287
pub shape: Vec<usize>,
8388
pub strides: Vec<isize>,
8489
}
8590

86-
pub(crate) trait ResizeGuard<'a> {
87-
type Resizable: 'a;
88-
fn try_resizable(&'a self, vm: &VirtualMachine) -> PyResult<Self::Resizable>;
91+
impl BufferOptions {
92+
pub const DEFAULT: Self = BufferOptions {
93+
readonly: true,
94+
len: 0,
95+
itemsize: 1,
96+
contiguous: true,
97+
format: Cow::Borrowed("B"),
98+
ndim: 1,
99+
shape: Vec::new(),
100+
strides: Vec::new(),
101+
};
89102
}
90103

91104
impl Default for BufferOptions {
92105
fn default() -> Self {
93-
BufferOptions {
94-
readonly: true,
95-
len: 0,
96-
itemsize: 1,
97-
contiguous: true,
98-
format: "B".to_owned(),
99-
ndim: 1,
100-
shape: Vec::new(),
101-
strides: Vec::new(),
102-
}
106+
Self::DEFAULT
103107
}
104108
}
105109

110+
pub(crate) trait ResizeGuard<'a> {
111+
type Resizable: 'a;
112+
fn try_resizable(&'a self, vm: &VirtualMachine) -> PyResult<Self::Resizable>;
113+
}
114+
106115
#[derive(FromArgs)]
107116
struct PyMemoryViewNewArgs {
108117
#[pyarg(any)]

vm/src/stdlib/array.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl fmt::Display for ArrayTypeSpecifierError {
3737
}
3838

3939
macro_rules! def_array_enum {
40-
($(($n:ident, $t:ty, $c:literal)),*$(,)?) => {
40+
($(($n:ident, $t:ty, $c:literal, $scode:literal)),*$(,)?) => {
4141
#[derive(Debug, Clone)]
4242
pub(crate) enum ArrayContentType {
4343
$($n(Vec<$t>),)*
@@ -58,6 +58,12 @@ macro_rules! def_array_enum {
5858
}
5959
}
6060

61+
fn typecode_str(&self) -> &'static str {
62+
match self {
63+
$(ArrayContentType::$n(_) => $scode,)*
64+
}
65+
}
66+
6167
fn itemsize(&self) -> usize {
6268
match self {
6369
$(ArrayContentType::$n(_) => std::mem::size_of::<$t>(),)*
@@ -394,19 +400,19 @@ macro_rules! def_array_enum {
394400
}
395401

396402
def_array_enum!(
397-
(SignedByte, i8, 'b'),
398-
(UnsignedByte, u8, 'B'),
403+
(SignedByte, i8, 'b', "b"),
404+
(UnsignedByte, u8, 'B', "B"),
399405
// TODO: support unicode char
400-
(SignedShort, raw::c_short, 'h'),
401-
(UnsignedShort, raw::c_ushort, 'H'),
402-
(SignedInt, raw::c_int, 'i'),
403-
(UnsignedInt, raw::c_uint, 'I'),
404-
(SignedLong, raw::c_long, 'l'),
405-
(UnsignedLong, raw::c_ulong, 'L'),
406-
(SignedLongLong, raw::c_longlong, 'q'),
407-
(UnsignedLongLong, raw::c_ulonglong, 'Q'),
408-
(Float, f32, 'f'),
409-
(Double, f64, 'd'),
406+
(SignedShort, raw::c_short, 'h', "h"),
407+
(UnsignedShort, raw::c_ushort, 'H', "H"),
408+
(SignedInt, raw::c_int, 'i', "i"),
409+
(UnsignedInt, raw::c_uint, 'I', "I"),
410+
(SignedLong, raw::c_long, 'l', "l"),
411+
(UnsignedLong, raw::c_ulong, 'L', "L"),
412+
(SignedLongLong, raw::c_longlong, 'q', "q"),
413+
(UnsignedLongLong, raw::c_ulonglong, 'Q', "Q"),
414+
(Float, f32, 'f', "f"),
415+
(Double, f64, 'd', "d"),
410416
);
411417

412418
trait ArrayElement: Sized {
@@ -875,7 +881,7 @@ impl Buffer for PyArrayRef {
875881
readonly: false,
876882
len: array.len(),
877883
itemsize: array.itemsize(),
878-
format: array.typecode().to_string(),
884+
format: array.typecode_str().into(),
879885
..Default::default()
880886
}));
881887
PyRwLockWriteGuard::downgrade(w)

0 commit comments

Comments
 (0)