Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl PyStr {
radd?.call((zelf,), vm)
} else {
Err(vm.new_type_error(format!(
"can only concatenate str (not \"{}\") to str",
r#"can only concatenate str (not "{}") to str"#,
other.class().name()
)))
}
Expand Down Expand Up @@ -570,6 +570,7 @@ impl PyStr {
hash => hash,
}
}

#[cold]
fn _compute_hash(&self, vm: &VirtualMachine) -> hash::PyHash {
let hash_val = vm.state.hash_secret.hash_bytes(self.as_bytes());
Expand All @@ -583,6 +584,7 @@ impl PyStr {
pub fn byte_len(&self) -> usize {
self.data.len()
}

#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
Expand Down Expand Up @@ -1439,6 +1441,7 @@ impl PyStr {
struct CharLenStr<'a>(&'a str, usize);
impl std::ops::Deref for CharLenStr<'_> {
type Target = str;

fn deref(&self) -> &Self::Target {
self.0
}
Expand Down Expand Up @@ -1852,6 +1855,7 @@ impl AnyStrWrapper<Wtf8> for PyStrRef {
fn as_ref(&self) -> Option<&Wtf8> {
Some(self.as_wtf8())
}

fn is_empty(&self) -> bool {
self.data.is_empty()
}
Expand All @@ -1861,6 +1865,7 @@ impl AnyStrWrapper<str> for PyStrRef {
fn as_ref(&self) -> Option<&str> {
self.data.as_str()
}

fn is_empty(&self) -> bool {
self.data.is_empty()
}
Expand All @@ -1870,6 +1875,7 @@ impl AnyStrWrapper<AsciiStr> for PyStrRef {
fn as_ref(&self) -> Option<&AsciiStr> {
self.data.as_ascii()
}

fn is_empty(&self) -> bool {
self.data.is_empty()
}
Expand All @@ -1893,9 +1899,11 @@ impl anystr::AnyChar for char {
fn is_lowercase(self) -> bool {
self.is_lowercase()
}

fn is_uppercase(self) -> bool {
self.is_uppercase()
}

fn bytes_len(self) -> usize {
self.len_utf8()
}
Expand Down Expand Up @@ -2122,9 +2130,11 @@ impl anystr::AnyChar for ascii::AsciiChar {
fn is_lowercase(self) -> bool {
self.is_lowercase()
}

fn is_uppercase(self) -> bool {
self.is_uppercase()
}

fn bytes_len(self) -> usize {
1
}
Expand Down
8 changes: 6 additions & 2 deletions vm/src/function/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,14 @@ pub trait FromArgOptional {
type Inner: TryFromObject;
fn from_inner(x: Self::Inner) -> Self;
}

impl<T: TryFromObject> FromArgOptional for OptionalArg<T> {
type Inner = T;
fn from_inner(x: T) -> Self {
Self::Present(x)
}
}

impl<T: TryFromObject> FromArgOptional for T {
type Inner = Self;
fn from_inner(x: Self) -> Self {
Expand Down Expand Up @@ -342,7 +344,7 @@ where
}

impl<T> KwArgs<T> {
pub fn new(map: IndexMap<String, T>) -> Self {
pub const fn new(map: IndexMap<String, T>) -> Self {
KwArgs(map)
}

Expand All @@ -354,11 +356,13 @@ impl<T> KwArgs<T> {
self.0.is_empty()
}
}

impl<T> FromIterator<(String, T)> for KwArgs<T> {
fn from_iter<I: IntoIterator<Item = (String, T)>>(iter: I) -> Self {
KwArgs(iter.into_iter().collect())
}
}

impl<T> Default for KwArgs<T> {
fn default() -> Self {
KwArgs(IndexMap::new())
Expand Down Expand Up @@ -408,7 +412,7 @@ where
}

impl<T> PosArgs<T> {
pub fn new(args: Vec<T>) -> Self {
pub const fn new(args: Vec<T>) -> Self {
Self(args)
}

Expand Down
8 changes: 4 additions & 4 deletions vm/src/function/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ impl ArgBytesLike {
f(&self.borrow_buf())
}

pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.0.desc.len
}

pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

Expand Down Expand Up @@ -103,11 +103,11 @@ impl ArgMemoryBuffer {
f(&mut self.borrow_buf_mut())
}

pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.0.desc.len
}

pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
}
Expand Down
1 change: 1 addition & 0 deletions vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ pub struct PyObject(PyInner<Erased>);

impl Deref for PyObjectRef {
type Target = PyObject;

#[inline(always)]
fn deref(&self) -> &PyObject {
unsafe { self.ptr.as_ref() }
Expand Down
4 changes: 4 additions & 0 deletions vm/src/object/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ where
fmt::Display::fmt(&**self, f)
}
}

impl<T: fmt::Display> fmt::Display for Py<T>
where
T: PyObjectPayload + fmt::Display,
Expand All @@ -72,6 +73,7 @@ impl<T: PyPayload> PyExact<T> {

impl<T: PyPayload> Deref for PyExact<T> {
type Target = Py<T>;

#[inline(always)]
fn deref(&self) -> &Py<T> {
&self.inner
Expand Down Expand Up @@ -108,6 +110,7 @@ impl<T: PyObjectPayload> AsRef<Py<T>> for PyExact<T> {

impl<T: PyPayload> std::borrow::ToOwned for PyExact<T> {
type Owned = PyRefExact<T>;

fn to_owned(&self) -> Self::Owned {
let owned = self.inner.to_owned();
unsafe { PyRefExact::new_unchecked(owned) }
Expand Down Expand Up @@ -181,6 +184,7 @@ impl<T: PyPayload> TryFromObject for PyRefExact<T> {

impl<T: PyPayload> Deref for PyRefExact<T> {
type Target = PyExact<T>;

#[inline(always)]
fn deref(&self) -> &PyExact<T> {
unsafe { PyExact::ref_unchecked(self.inner.deref()) }
Expand Down
1 change: 1 addition & 0 deletions vm/src/object/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ unsafe impl<A: Traverse> Traverse for (A,) {
self.0.traverse(tracer_fn);
}
}

trace_tuple!((A, 0), (B, 1));
trace_tuple!((A, 0), (B, 1), (C, 2));
trace_tuple!((A, 0), (B, 1), (C, 2), (D, 3));
Expand Down
7 changes: 1 addition & 6 deletions vm/src/protocol/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,7 @@ impl BufferDescriptor {
}

pub fn is_zero_in_shape(&self) -> bool {
for (shape, _, _) in self.dim_desc.iter().cloned() {
if shape == 0 {
return true;
}
}
false
self.dim_desc.iter().any(|(shape, _, _)| *shape == 0)
}

// TODO: support fortain order
Expand Down
3 changes: 2 additions & 1 deletion vm/src/protocol/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ where
O: Borrow<PyObject>,
{
type Target = PyObject;

#[inline(always)]
fn deref(&self) -> &Self::Target {
self.0.borrow()
Expand Down Expand Up @@ -242,7 +243,7 @@ impl<'a, T, O> PyIterIter<'a, T, O>
where
O: Borrow<PyObject>,
{
pub fn new(vm: &'a VirtualMachine, obj: O, length_hint: Option<usize>) -> Self {
pub const fn new(vm: &'a VirtualMachine, obj: O, length_hint: Option<usize>) -> Self {
Self {
vm,
obj,
Expand Down
4 changes: 2 additions & 2 deletions vm/src/protocol/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub type PyNumberTernaryFunc = fn(&PyObject, &PyObject, &PyObject, &VirtualMachi

impl PyObject {
#[inline]
pub fn to_number(&self) -> PyNumber<'_> {
pub const fn to_number(&self) -> PyNumber<'_> {
PyNumber(self)
}

Expand Down Expand Up @@ -440,7 +440,7 @@ impl Deref for PyNumber<'_> {
}

impl<'a> PyNumber<'a> {
pub(crate) fn obj(self) -> &'a PyObject {
pub(crate) const fn obj(self) -> &'a PyObject {
self.0
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ impl PyComparisonOp {
}
}

pub fn operator_token(self) -> &'static str {
pub const fn operator_token(self) -> &'static str {
match self {
Self::Lt => "<",
Self::Le => "<=",
Expand Down