Skip to content

Commit

Permalink
took out changes to any casting
Browse files Browse the repository at this point in the history
  • Loading branch information
Waidhoferj committed Jul 21, 2022
1 parent 8e483cc commit 8d82dc1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[build-system]
requires = ["maturin>=0.13"]
requires = ["maturin>=0.13,<0.14"]
build-backend = "maturin"
60 changes: 1 addition & 59 deletions src/shared_types.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -74,16 +68,6 @@ impl<I, P> SharedType<I, P> {
}
}

impl<I, P: Default> SharedType<I, P> {
/// Extracts the preliminary value if it exists and replaces it with a default value.
fn take_prelim(&mut self) -> Option<P> {
match self {
SharedType::Integrated(_) => None,
SharedType::Prelim(p) => Some(std::mem::take(p)),
}
}
}

#[derive(FromPyObject)]
pub enum Shared {
Text(Py<YText>),
Expand Down Expand Up @@ -148,45 +132,3 @@ impl TryFrom<PyObject> for Shared {
})
}
}

impl TryFrom<Shared> for Any {
type Error = PyErr;

fn try_from(shared: Shared) -> Result<Self, Self::Error> {
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<Vec<Any>, _> =
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<HashMap<String, Any>> = 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}"
)))
}
}
}
6 changes: 5 additions & 1 deletion src/type_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl ToPython for &Change {
}
}

#[repr(transparent)]
struct EntryChangeWrapper<'a>(&'a EntryChange);

impl<'a> IntoPy<PyObject> for EntryChangeWrapper<'a> {
Expand Down Expand Up @@ -143,6 +144,7 @@ impl<'a> IntoPy<PyObject> for EntryChangeWrapper<'a> {
}
}

#[repr(transparent)]
pub(crate) struct PyObjectWrapper(pub PyObject);

impl Prelim for PyObjectWrapper {
Expand Down Expand Up @@ -258,7 +260,9 @@ impl TryFrom<PyObjectWrapper> 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}"
Expand Down

0 comments on commit 8d82dc1

Please sign in to comment.