Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cow equivalent in PyO3 #3839

Open
cmpute opened this issue Feb 15, 2024 · 1 comment
Open

Cow equivalent in PyO3 #3839

cmpute opened this issue Feb 15, 2024 · 1 comment

Comments

@cmpute
Copy link
Contributor

cmpute commented Feb 15, 2024

I propose to add a type to support copy-on-write behavior just like the std::borrow::Cow. The Cow type in std is usually unwanted because a reference to a Python object is held though PyRef. Therefore something like the following will be useful:

enum PyCow<'a, B>
where
    B: 'a + ToOwned + PyClass,
{
    Borrowed(PyRef<'a, B>),
    Owned(<B as ToOwned>::Owned),
}

This is useful when a function accepts both a Python object or something that can be converted to the object. For example, if I have a rust-implemented Python-type Decimal, a function that accepts a either Decimal or a string can take this as input to prevent a copy.

(Not sure if this is useful for other people, or if it's compatible with the new Bound API or not... Just post the idea here for discussion)

@davidhewitt
Copy link
Member

I think there is definitely space for this kind of idea, and it is strongly related to what we just added in #3802 . There's a lot of unresolved questions in my head about this, though.

  • is it limited to B: PyClass or B: PyTypeInfo?
  • does it store PyRef or Bound for the python variant?
  • does it try to add a third &B variant to become a complete superset of normal Cow?

Folks willing to explore this idea further are very welcome, whether it gets merged immediately into pyo3 or we allow time for this idea to mature as a separate crate, I can see potential in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants