Skip to content

#166 always goes the slow path for most types #234

@feefladder

Description

@feefladder

bytemuck's try_cast_vec for Vec<u8>->Vec<u16> or any different alignment will fail, regardless of whether the allocation is aligned. It is undefined behaviour in rust to deallocate with a different alignment than the allocation. So this code will always take the second path. That could be changed to

Ok(TypedArray::UInt16(
    try_cast_slice(data[..])
    .map(|s| s.to_vec())  // this copy already always happens
    .unwrap_or_else(
                    |(_, data)| {
                        // Fallback to manual conversion when not aligned
                        data.chunks_exact(2)
                            .map(|b| u16::from_ne_bytes([b[0], b[1]]))
                            .collect()
                    },
                )))

which will go the first path if the allocation is aligned.

Alternatively, this copy could be avoided with some notes in #203

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions