-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tagged union serializer 🚀 (#1397)
- Loading branch information
1 parent
3d8295e
commit fdd1e85
Showing
12 changed files
with
430 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod union; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::{PyTraverseError, PyVisit}; | ||
|
||
use crate::lookup_key::LookupKey; | ||
use crate::py_gc::PyGcTraverse; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Discriminator { | ||
/// use `LookupKey` to find the tag, same as we do to find values in typed_dict aliases | ||
LookupKey(LookupKey), | ||
/// call a function to find the tag to use | ||
Function(PyObject), | ||
} | ||
|
||
impl Discriminator { | ||
pub fn new(py: Python, raw: &Bound<'_, PyAny>) -> PyResult<Self> { | ||
if raw.is_callable() { | ||
return Ok(Self::Function(raw.to_object(py))); | ||
} | ||
|
||
let lookup_key = LookupKey::from_py(py, raw, None)?; | ||
Ok(Self::LookupKey(lookup_key)) | ||
} | ||
|
||
pub fn to_string_py(&self, py: Python) -> PyResult<String> { | ||
match self { | ||
Self::Function(f) => Ok(format!("{}()", f.getattr(py, "__name__")?)), | ||
Self::LookupKey(lookup_key) => Ok(lookup_key.to_string()), | ||
} | ||
} | ||
} | ||
|
||
impl PyGcTraverse for Discriminator { | ||
fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> { | ||
match self { | ||
Self::Function(obj) => visit.call(obj)?, | ||
Self::LookupKey(_) => {} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub(crate) const SMALL_UNION_THRESHOLD: usize = 4; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.