Skip to content

Commit dfd3d57

Browse files
authored
Merge pull request #3310 from youknowone/as-mapping-never-fail
as_mapping never fails
2 parents 8f404c7 + 77e33c5 commit dfd3d57

File tree

12 files changed

+42
-37
lines changed

12 files changed

+42
-37
lines changed

stdlib/src/array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,12 +1253,12 @@ mod array {
12531253
}
12541254

12551255
impl AsMapping for PyArray {
1256-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
1257-
Ok(PyMappingMethods {
1256+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
1257+
PyMappingMethods {
12581258
length: Some(Self::length),
12591259
subscript: Some(Self::subscript),
12601260
ass_subscript: Some(Self::ass_subscript),
1261-
})
1261+
}
12621262
}
12631263

12641264
fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {

vm/src/builtins/bytearray.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,12 +756,12 @@ impl<'a> BufferResizeGuard<'a> for PyByteArray {
756756
}
757757

758758
impl AsMapping for PyByteArray {
759-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
760-
Ok(PyMappingMethods {
759+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
760+
PyMappingMethods {
761761
length: Some(Self::length),
762762
subscript: Some(Self::subscript),
763763
ass_subscript: Some(Self::ass_subscript),
764-
})
764+
}
765765
}
766766

767767
#[inline]

vm/src/builtins/bytes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,12 @@ impl BufferInternal for PyRef<PyBytes> {
569569
}
570570

571571
impl AsMapping for PyBytes {
572-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
573-
Ok(PyMappingMethods {
572+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
573+
PyMappingMethods {
574574
length: Some(Self::length),
575575
subscript: Some(Self::subscript),
576576
ass_subscript: None,
577-
})
577+
}
578578
}
579579

580580
#[inline]

vm/src/builtins/dict.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,12 @@ impl PyDict {
416416
}
417417

418418
impl AsMapping for PyDict {
419-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
420-
Ok(PyMappingMethods {
419+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
420+
PyMappingMethods {
421421
length: Some(Self::length),
422422
subscript: Some(Self::subscript),
423423
ass_subscript: Some(Self::ass_subscript),
424-
})
424+
}
425425
}
426426

427427
#[inline]

vm/src/builtins/list.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,12 @@ impl PyList {
418418
}
419419

420420
impl AsMapping for PyList {
421-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
422-
Ok(PyMappingMethods {
421+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
422+
PyMappingMethods {
423423
length: Some(Self::length),
424424
subscript: Some(Self::subscript),
425425
ass_subscript: Some(Self::ass_subscript),
426-
})
426+
}
427427
}
428428

429429
#[inline]

vm/src/builtins/mappingproxy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ impl PyMappingProxy {
151151
}
152152

153153
impl AsMapping for PyMappingProxy {
154-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
155-
Ok(PyMappingMethods {
154+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
155+
PyMappingMethods {
156156
length: None,
157157
subscript: Some(Self::subscript),
158158
ass_subscript: None,
159-
})
159+
}
160160
}
161161

162162
#[inline]

vm/src/builtins/memory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,12 @@ impl BufferInternal for PyRef<PyMemoryView> {
737737
}
738738

739739
impl AsMapping for PyMemoryView {
740-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
741-
Ok(PyMappingMethods {
740+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
741+
PyMappingMethods {
742742
length: Some(Self::length),
743743
subscript: Some(Self::subscript),
744744
ass_subscript: Some(Self::ass_subscript),
745-
})
745+
}
746746
}
747747

748748
#[inline]

vm/src/builtins/range.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,12 @@ impl PyRange {
377377
}
378378

379379
impl AsMapping for PyRange {
380-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
381-
Ok(PyMappingMethods {
380+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
381+
PyMappingMethods {
382382
length: Some(Self::length),
383383
subscript: Some(Self::subscript),
384384
ass_subscript: None,
385-
})
385+
}
386386
}
387387

388388
#[inline]

vm/src/builtins/tuple.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,12 @@ impl PyTuple {
306306
}
307307

308308
impl AsMapping for PyTuple {
309-
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
310-
Ok(PyMappingMethods {
309+
fn as_mapping(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
310+
PyMappingMethods {
311311
length: Some(Self::length),
312312
subscript: Some(Self::subscript),
313313
ass_subscript: None,
314-
})
314+
}
315315
}
316316

317317
#[inline]

vm/src/protocol/mapping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl PyMapping<PyObjectRef> {
3636
let obj_cls = self.0.class();
3737
for cls in obj_cls.iter_mro() {
3838
if let Some(f) = cls.slots.as_mapping.load() {
39-
return f(&self.0, vm).unwrap();
39+
return f(&self.0, vm);
4040
}
4141
}
4242
PyMappingMethods::default()

vm/src/pyobjectrc.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ impl PyObjectRef {
237237
PyRef::from_obj_unchecked(self)
238238
}
239239

240+
/// # Safety
241+
/// T must be the exact payload type
242+
pub unsafe fn downcast_unchecked_ref<T: PyObjectPayload>(&self) -> &PyRef<T> {
243+
debug_assert!(self.payload_is::<T>());
244+
&*(self as *const PyObjectRef as *const PyRef<T>)
245+
}
246+
240247
pub(crate) fn class_lock(&self) -> &PyRwLock<PyTypeRef> {
241248
&self.rc.inner.typ
242249
}

vm/src/types/slot.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Default for PyTypeFlags {
128128
}
129129

130130
pub(crate) type GenericMethod = fn(&PyObjectRef, FuncArgs, &VirtualMachine) -> PyResult;
131-
pub(crate) type AsMappingFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<PyMappingMethods>;
131+
pub(crate) type AsMappingFunc = fn(&PyObjectRef, &VirtualMachine) -> PyMappingMethods;
132132
pub(crate) type HashFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<PyHash>;
133133
// CallFunc = GenericMethod
134134
pub(crate) type GetattroFunc = fn(PyObjectRef, PyStrRef, &VirtualMachine) -> PyResult;
@@ -150,7 +150,7 @@ pub(crate) type DescrSetFunc =
150150
pub(crate) type NewFunc = fn(PyTypeRef, FuncArgs, &VirtualMachine) -> PyResult;
151151
pub(crate) type DelFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult<()>;
152152

153-
fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
153+
fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyMappingMethods {
154154
macro_rules! then_some_closure {
155155
($cond:expr, $closure:expr) => {
156156
if $cond {
@@ -160,7 +160,7 @@ fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyResult<PyMa
160160
}
161161
};
162162
}
163-
Ok(PyMappingMethods {
163+
PyMappingMethods {
164164
length: then_some_closure!(zelf.has_class_attr("__len__"), |zelf, vm| {
165165
vm.call_special_method(zelf, "__len__", ()).map(|obj| {
166166
obj.payload_if_subclass::<PyInt>(vm)
@@ -189,7 +189,7 @@ fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyResult<PyMa
189189
.map(|_| Ok(()))?,
190190
}
191191
),
192-
})
192+
}
193193
}
194194

195195
fn hash_wrapper(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
@@ -763,10 +763,8 @@ pub trait AsBuffer: PyValue {
763763
pub trait AsMapping: PyValue {
764764
#[inline]
765765
#[pyslot]
766-
fn slot_as_mapping(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult<PyMappingMethods> {
767-
let zelf = zelf
768-
.downcast_ref()
769-
.ok_or_else(|| vm.new_type_error("unexpected payload for as_mapping".to_owned()))?;
766+
fn slot_as_mapping(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyMappingMethods {
767+
let zelf = unsafe { zelf.downcast_unchecked_ref::<Self>() };
770768
Self::as_mapping(zelf, vm)
771769
}
772770

@@ -792,7 +790,7 @@ pub trait AsMapping: PyValue {
792790
})
793791
}
794792

795-
fn as_mapping(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyMappingMethods>;
793+
fn as_mapping(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyMappingMethods;
796794

797795
fn length(zelf: PyObjectRef, _vm: &VirtualMachine) -> PyResult<usize>;
798796

0 commit comments

Comments
 (0)