Skip to content

Casts and conversions for owned, boxed slices #238

Open
@Pr0methean

Description

@Pr0methean

Owned slices can be cast with fewer restrictions than mutably borrowed ones, because the cast doesn't have to be reversible. This issue is a request for two features:

  • try_cast_boxed_slice: casts Box<[A]> to Box<[B]> if they have the same layout and B is AnyBitPattern.
  • convert_boxed_slice: converts Box<[A]> to Box<[B]> if they have the same layout, and takes a Fn(A) -> B parameter. (Could be implemented by first casting to a slice of a union type.)

An example use case for convert_box_slice would be to create a safe version of this function, although it would also require ColorU8, the output type, to implement AnyBitPattern. Note that the input type PremultipliedColorU8 cannot implement AnyBitPattern, since it has the invariant that no color byte has a larger value than the alpha byte. ColorU8 can implement AnyBitPattern but doesn't.

https://github.com/Pr0methean/OcHd-RustBuild/blob/main/main/src/image_tasks/png_output.rs#L312-L324

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions