From 8f2420d83b68d13342888aab3bb417b90878dbef Mon Sep 17 00:00:00 2001 From: Andrey Chursin Date: Wed, 8 Sep 2021 16:49:51 -0700 Subject: [PATCH] cpython-ext: fix py-cell Summary: For some reason it got broken, need to call `as_ref()` to properly cast type Reviewed By: quark-zju Differential Revision: D30740629 fbshipit-source-id: f49275caae9d360859e97c03709a720dabc22e9e --- eden/scm/lib/cpython-ext/src/cell.rs | 29 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/eden/scm/lib/cpython-ext/src/cell.rs b/eden/scm/lib/cpython-ext/src/cell.rs index aa1d2fa06c..0d0b10dbf3 100644 --- a/eden/scm/lib/cpython-ext/src/cell.rs +++ b/eden/scm/lib/cpython-ext/src/cell.rs @@ -14,13 +14,15 @@ use std::any::Any; use std::cell::RefCell; use std::fmt::Debug; +type BoxedAny = Box; + // PyCell allows to put arbitrary rust data and pass it between different rust functions through python code // This allows to avoid using bincode or writing wrapper types for some basic use cases py_class!(pub class PyCell |py| { - data inner: RefCell>>; - data fmt: Box String) + Send + Sync>; - data fn_export: Box PyObject) + Send + Sync>; - data fn_import: Box PyResult>) + Send + Sync>; + data inner: RefCell>; + data fmt: Box String) + Send + Sync>; + data fn_export: Box PyObject) + Send + Sync>; + data fn_import: Box PyResult) + Send + Sync>; def __str__(&self) -> PyResult { let str = self.inner(py).borrow().as_ref().map(|inner| { @@ -53,20 +55,19 @@ impl PyCell { py: Python, data: T, ) -> PyResult { - let inner = Box::new(data) as Box; - let fmt = |obj: &(dyn Any)| { - let obj = obj.downcast_ref::().unwrap(); // does not fail + let inner = Box::new(data) as BoxedAny; + let fmt = |obj: &BoxedAny| { + let obj = obj.as_ref().downcast_ref::().unwrap(); // does not fail format!("{:?}", obj) }; - let export = |obj: &(dyn Any), py: Python| { - let obj = obj.downcast_ref::().unwrap(); // does not fail + let export = |obj: &BoxedAny, py: Python| { + let obj = obj.as_ref().downcast_ref::().unwrap(); // does not fail Serde(obj).to_py_object(py) }; - let import = - |obj: PyObject, py: Python| -> PyResult> { - let obj: Serde = Serde::extract(py, &obj)?; - Ok(Box::new(obj.0) as Box) - }; + let import = |obj: PyObject, py: Python| -> PyResult { + let obj: Serde = Serde::extract(py, &obj)?; + Ok(Box::new(obj.0) as BoxedAny) + }; Self::create_instance( py, RefCell::new(Some(inner)),