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

WIP/proof-of-concept: refactor Box/Rc/Arc to reduce code duplication. #134

Closed
wants to merge 2 commits into from

Conversation

zachs18
Copy link
Contributor

@zachs18 zachs18 commented Sep 2, 2022

WIP/proof-of-concept: As mentioned in #132, much of the code for Box/Rc/Arc's functions is very similar. This WIP does not address the public API duplication mentioned in that PR and #133, but it does somewhat address the code duplication by factoring out the common code into generic try_cast_aligned_ptr and try_cast_aligned_slice_ptr functions, which use a trait CastablePointer to convert between smart pointer types and raw pointers.

The API is the same as #132 and is all implemented, but there are some "TODO"s in the code of things that could be added or things that could be removed.

Currently, the only changes are private, but something similar this could perhaps be made public.

Alternately to how it currrently works, the CastablePointer<T> trait could instead be parameterized on the "raw" type it returns, (rather than being hardcoded to have a "raw" type of *mut T). This could make it possible to additionally use it for Vec, though I don't know how useful that would be, since Vec needs some special handling anyway.

cast_slice error precedence changed, so attempting to cast an unaligned *and* slop-ful slice will now return
OutputSliceWouldHaveSlop instead of TargetAlignmentGreaterAndInputNotAligned.

The documentation of try_cast_slice doesn't *explicitly* say which error will be returned if multiple occur,
so this might just be fine as a behavior change.
@zachs18
Copy link
Contributor Author

zachs18 commented Jul 21, 2024

Rebased, but I think I'm going to close this because:

  1. core::ptr::slice_from_raw_parts was stabilized in 1.42.0, after bytemuck's MSRV, so cast_{ref,mut}'s implementations (and cast_{slice,slice_mut}'s) wouldn't even be able to be deduplicated with this.
  2. while it does reduce code duplication, it also adds more code (most of which is comments, but still). Sometimes copy-pasting is more easier to understand than abstraction lol.

@zachs18 zachs18 closed this Jul 21, 2024
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.

1 participant