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

Add fn as_bytes to PyByteArray #632

Closed
calixteman opened this issue Oct 16, 2019 · 4 comments · Fixed by #967
Closed

Add fn as_bytes to PyByteArray #632

calixteman opened this issue Oct 16, 2019 · 4 comments · Fixed by #967

Comments

@calixteman
Copy link

I understand the rational for to_vec (#373 (comment)) but in my use case it won't never happen (I just need to read data and nothing will modified them).
So it'd be nice to avoid a useless copy here.

@programmerjake
Copy link
Contributor

maybe it would work to return &Cell<[u8]>, since the Python value is both shared and mutable

@programmerjake
Copy link
Contributor

&Cell<[u8]> would work, as far as I can tell, since it is both shared and mutable, and doesn't implement Send/Sync, so wouldn't be able to be passed into allow_threads, preventing access while the GIL is unlocked.

However, if Python interpreters are allowed to move the backing buffer during a garbage collection (or similar), then returning any reference wouldn't be safe, unless the buffer was somehow pinned. Not sure if interpreters are allowed to do that.

@davidhewitt
Copy link
Member

davidhewitt commented Jun 5, 2020

I think the bytearray can be resized at any point in python code just by using += operator, so returning a reference to data in the bytearray is definitely unsafe.

We can add pub fn data() -> *mut u8 which returns a pointer to the start of the backing buffer. It would need to have documented that the backing buffer may be relocated during any Python code execution so it's up to the user to tread carefully.

This would allow @calixteman to do what they want via the unsafe api std::slice::from_raw_parts.

@kngwyu
Copy link
Member

kngwyu commented Jun 5, 2020

We can add pub fn data() -> *mut u8 which returns a pointer to the start of the backing buffer.

👍 but I'm also for unsafe fn as_bytes(&self) -> &[u8]. It would be convenient.

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

Successfully merging a pull request may close this issue.

4 participants