From 8d82dc183fb5c6807d7a244bcb578e5fba91cdbd Mon Sep 17 00:00:00 2001 From: waidhoferj Date: Thu, 21 Jul 2022 15:35:30 -0700 Subject: [PATCH] took out changes to any casting --- pyproject.toml | 2 +- src/shared_types.rs | 60 +---------------------------------------- src/type_conversions.rs | 6 ++++- 3 files changed, 7 insertions(+), 61 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 573adf8..5d44682 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,3 @@ [build-system] -requires = ["maturin>=0.13"] +requires = ["maturin>=0.13,<0.14"] build-backend = "maturin" diff --git a/src/shared_types.rs b/src/shared_types.rs index 7fc060a..d660f78 100644 --- a/src/shared_types.rs +++ b/src/shared_types.rs @@ -1,18 +1,12 @@ use crate::{ - type_conversions::{MultipleIntegrationError, PyObjectWrapper}, y_array::YArray, y_map::YMap, y_text::YText, y_xml::{YXmlElement, YXmlText}, }; -use lib0::any::Any; use pyo3::create_exception; use pyo3::{exceptions::PyException, prelude::*}; -use std::{ - collections::HashMap, - convert::{TryFrom, TryInto}, - fmt::Display, -}; +use std::{convert::TryFrom, fmt::Display}; use yrs::types::TYPE_REFS_XML_TEXT; use yrs::types::{TypeRefs, TYPE_REFS_ARRAY, TYPE_REFS_MAP, TYPE_REFS_TEXT}; use yrs::{types::TYPE_REFS_XML_ELEMENT, SubscriptionId}; @@ -74,16 +68,6 @@ impl SharedType { } } -impl SharedType { - /// Extracts the preliminary value if it exists and replaces it with a default value. - fn take_prelim(&mut self) -> Option

{ - match self { - SharedType::Integrated(_) => None, - SharedType::Prelim(p) => Some(std::mem::take(p)), - } - } -} - #[derive(FromPyObject)] pub enum Shared { Text(Py), @@ -148,45 +132,3 @@ impl TryFrom for Shared { }) } } - -impl TryFrom for Any { - type Error = PyErr; - - fn try_from(shared: Shared) -> Result { - if shared.is_prelim() { - Python::with_gil(|py| { - match shared { - Shared::Text(text) => { - let content = text - .borrow_mut(py) - .0 - .take_prelim() - .unwrap() - .into_boxed_str(); - Ok(Any::String(content)) - } - - Shared::Array(array) => { - let content = array.borrow_mut(py).0.take_prelim().unwrap(); - let any_array: Result, _> = - content.into_iter().map(|v| PyObjectWrapper(v).try_into()).collect(); - Ok(Any::Array(any_array?.into_boxed_slice())) - } - Shared::Map(dict) => { - let content = dict.borrow_mut(py).0.take_prelim().unwrap(); - let any_dict: PyResult> = content - .into_iter() - .map(|(k, v)| PyObjectWrapper(v).try_into().map(|v| (k, v))) - .collect(); - Ok(Any::Map(Box::new(any_dict?))) - } - Shared::XmlElement(_) | Shared::XmlText(_) => unreachable!("As defined in Shared::is_prelim(), neither XML type can ever exist outside a YDoc"), - } - }) - } else { - Err(MultipleIntegrationError::new_err(format!( - "Cannot integrate a nested Ypy object because is already integrated into a YDoc: {shared}" - ))) - } - } -} diff --git a/src/type_conversions.rs b/src/type_conversions.rs index b508738..12064c9 100644 --- a/src/type_conversions.rs +++ b/src/type_conversions.rs @@ -114,6 +114,7 @@ impl ToPython for &Change { } } +#[repr(transparent)] struct EntryChangeWrapper<'a>(&'a EntryChange); impl<'a> IntoPy for EntryChangeWrapper<'a> { @@ -143,6 +144,7 @@ impl<'a> IntoPy for EntryChangeWrapper<'a> { } } +#[repr(transparent)] pub(crate) struct PyObjectWrapper(pub PyObject); impl Prelim for PyObjectWrapper { @@ -258,7 +260,9 @@ impl TryFrom for Any { .collect(); result.map(|res| Any::Map(Box::new(res))) } else if let Ok(v) = Shared::try_from(PyObject::from(v)) { - v.try_into() + Err(MultipleIntegrationError::new_err(format!( + "Cannot integrate a nested Ypy object because is already integrated into a YDoc: {v}" + ))) } else { Err(PyTypeError::new_err(format!( "Cannot integrate this type into a YDoc: {v}"