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 Buffer type field as PhantomData. #1

Merged
merged 1 commit into from
Jan 15, 2021
Merged

Add Buffer type field as PhantomData. #1

merged 1 commit into from
Jan 15, 2021

Conversation

jsatka
Copy link

@jsatka jsatka commented Jan 14, 2021

Add PhantomData type to Buffer indicating the type of data the Buffer points to. There is an example in Unused type parameter section of the PhantomData marker docs.

The sole purpose of the type field is to help Rust compiler's type checking, preventing accidentally reading to result array of another type or writing data with different type using CommandQueue's enqueue_ methods. The field is hidden in docs becaue it has size of zero bytes i.e. it doesn't exist at runtime. This hiding in the docs has been the choice e.g. in euclid math tools crate.

If data with different type needs to be written or read into result array of another type however, the type parameter can be explicitly cast with the cast() method, which essentially creates a clone of the Buffer with the new type. Although other methods for achieving the same would exist, such as casting the data to correct type before writing or after reading to result array.

@jsatka jsatka changed the title Add Buffer phantom type parameter. Add Buffer type parameter as PhantomData. Jan 14, 2021
@jsatka jsatka changed the title Add Buffer type parameter as PhantomData. Add Buffer type field as PhantomData. Jan 14, 2021
@kenba kenba added the enhancement New feature or request label Jan 15, 2021
@kenba kenba merged commit 8544ec5 into kenba:main Jan 15, 2021
@kenba
Copy link
Owner

kenba commented Jan 15, 2021

Thank you jsatka, that's a good improvement.
I'll raise an issue to consider adding PhantomData to the other memory objects.

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

Successfully merging this pull request may close these issues.

2 participants