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

implement for &[T] and &mut [T] #2

Merged
merged 1 commit into from
Dec 9, 2021
Merged

Conversation

Stebalien
Copy link
Contributor

Implementing for references over arbitrary unsized types doesn't work because it may not be safe to cast between them (I think?). But it's definitely safe to do this for [T].

This makes it possible to perform casts like cast!(some_slice, &[u8]) (useful for optimizing for byte arrays).

Implementing for references over arbitrary unsized types doesn't work
because it may not be safe to cast between them (I think?). But it's
definitely safe to do this for [T].

This makes it possible to perform casts like `cast!(some_slice,
&[u8])` (useful for optimizing for byte arrays).
@sagebind
Copy link
Owner

sagebind commented Dec 9, 2021

Nice addition! You're right, this seems to be sound from my assessment and could be a really useful feature. Thanks for the PR!

@sagebind
Copy link
Owner

sagebind commented Dec 9, 2021

Implementing for references over arbitrary unsized types doesn't work because it may not be safe to cast between them (I think?).

The main problem with unsized types is that it becomes more difficult to statically verify that two types are equal. We have checks on the size and alignment because TypeId can have collisions in rare scenarios, but we can't use those safeguards on unsized types.

Aside from that, it would be safe to cast a type where T: ?Sized + 'static. Perhaps it could be something we could support in the future more generally -- it could be added without breaking compatibility with this PR.

@sagebind sagebind merged commit 59c9713 into sagebind:master Dec 9, 2021
@sagebind
Copy link
Owner

sagebind commented Dec 9, 2021

This has been published in version 0.1.2.

@Stebalien
Copy link
Contributor Author

Aside from that, it would be safe to cast a type where T: ?Sized + 'static. Perhaps it could be something we could support in the future more generally -- it could be added without breaking compatibility with this PR.

That exceeds my rust-fu for now, unfortunately.

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 this pull request may close these issues.

2 participants