Skip to content

Commit b1b028e

Browse files
committed
Change mem::uninitialized to mem::MaybeUninit
1 parent 52f1965 commit b1b028e

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn remove_importlib_frames_inner(
106106
tb: Option<PyTracebackRef>,
107107
always_trim: bool,
108108
) -> Option<PyTracebackRef> {
109-
let traceback = tb.as_ref()?;;
109+
let traceback = tb.as_ref()?;
110110
let file_name = traceback.frame.code.source_path.to_string();
111111
if (file_name == "_frozen_importlib" || file_name == "_frozen_importlib_external")
112112
&& (always_trim || traceback.frame.code.obj_name == "_call_with_frames_removed")

vm/src/obj/objmemory.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::borrow::Borrow;
2-
31
use super::objbyteinner::try_as_byte;
42
use super::objtype::{issubclass, PyClassRef};
53
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
@@ -26,7 +24,7 @@ impl PyMemoryView {
2624
bytes_object: PyObjectRef,
2725
vm: &VirtualMachine,
2826
) -> PyResult<PyMemoryViewRef> {
29-
let object_type = bytes_object.typ.borrow();
27+
let object_type = unsafe { &*bytes_object.typ.as_ptr() };
3028

3129
if issubclass(object_type, &vm.ctx.types.memoryview_type)
3230
|| issubclass(object_type, &vm.ctx.types.bytes_type)

vm/src/obj/objtype.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::cell::RefCell;
22
use std::collections::HashMap;
33
use std::fmt;
4+
use std::mem::MaybeUninit;
45

56
use super::objdict::PyDictRef;
67
use super::objlist::PyList;
@@ -505,7 +506,7 @@ pub fn new(
505506
slots: RefCell::default(),
506507
},
507508
dict: None,
508-
typ,
509+
typ: MaybeUninit::new(typ),
509510
}
510511
.into_ref();
511512

vm/src/pyobject.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::cell::Cell;
33
use std::collections::HashMap;
44
use std::fmt;
55
use std::marker::PhantomData;
6+
use std::mem::MaybeUninit;
67
use std::ops::Deref;
78
use std::rc::Rc;
89

@@ -521,7 +522,7 @@ impl PyContext {
521522

522523
pub fn new_instance(&self, class: PyClassRef, dict: Option<PyDictRef>) -> PyObjectRef {
523524
PyObject {
524-
typ: class,
525+
typ: MaybeUninit::new(class),
525526
dict,
526527
payload: objobject::PyInstance,
527528
}
@@ -565,7 +566,7 @@ pub struct PyObject<T>
565566
where
566567
T: ?Sized + PyObjectPayload,
567568
{
568-
pub typ: PyClassRef,
569+
pub typ: MaybeUninit<PyClassRef>,
569570
pub dict: Option<PyDictRef>, // __dict__ member
570571
pub payload: T,
571572
}
@@ -792,13 +793,13 @@ where
792793
T: ?Sized + PyObjectPayload,
793794
{
794795
fn class(&self) -> PyClassRef {
795-
self.typ.clone()
796+
unsafe { (*self.typ.as_ptr()).clone() }
796797
}
797798
}
798799

799800
impl<T> TypeProtocol for PyRef<T> {
800801
fn class(&self) -> PyClassRef {
801-
self.obj.typ.clone()
802+
unsafe { (*self.obj.typ.as_ptr()).clone() }
802803
}
803804
}
804805

@@ -1038,7 +1039,12 @@ where
10381039
{
10391040
#[allow(clippy::new_ret_no_self)]
10401041
pub fn new(payload: T, typ: PyClassRef, dict: Option<PyDictRef>) -> PyObjectRef {
1041-
PyObject { typ, dict, payload }.into_ref()
1042+
PyObject {
1043+
typ: MaybeUninit::new(typ),
1044+
dict,
1045+
payload,
1046+
}
1047+
.into_ref()
10421048
}
10431049

10441050
// Move this object into a reference object, transferring ownership.

vm/src/types.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ use crate::obj::objweakref;
3737
use crate::obj::objzip;
3838
use crate::pyobject::{PyAttributes, PyContext, PyObject, PyObjectRef};
3939
use std::cell::RefCell;
40-
use std::mem;
41-
use std::ptr;
40+
use std::mem::MaybeUninit;
4241

4342
/// Holder of references to builtin types.
4443
#[derive(Debug)]
@@ -232,7 +231,7 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
232231
// (and yes, this will never get dropped. TODO?)
233232
let (type_type, object_type) = unsafe {
234233
let object_type = PyObject {
235-
typ: mem::uninitialized(), // !
234+
typ: MaybeUninit::uninit(), // !
236235
dict: None,
237236
payload: PyClass {
238237
name: String::from("object"),
@@ -246,7 +245,7 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
246245
.into_ref();
247246

248247
let type_type = PyObject {
249-
typ: mem::uninitialized(), // !
248+
typ: MaybeUninit::uninit(), // !
250249
dict: None,
251250
payload: PyClass {
252251
name: String::from("type"),
@@ -265,8 +264,8 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
265264
let type_type: PyClassRef = type_type.downcast().unwrap();
266265
let object_type: PyClassRef = object_type.downcast().unwrap();
267266

268-
ptr::write(&mut (*object_type_ptr).typ, type_type.clone());
269-
ptr::write(&mut (*type_type_ptr).typ, type_type.clone());
267+
(*object_type_ptr).typ.as_mut_ptr().write(type_type.clone());
268+
(*type_type_ptr).typ.as_mut_ptr().write(type_type.clone());
270269

271270
(type_type, object_type)
272271
};

0 commit comments

Comments
 (0)