Skip to content

Buffer::bit_slice has incorrect length for aligned offset #807

@bjchambers

Description

@bjchambers

Describe the bug
Because buf.bit_slice(...) falls back to a slice that ignores the length when the offset is byte-aligned, the resulting buffer has an incorrect length. This leads to problems when applying bitwise operations such as &, for instance.

To Reproduce

The following test fails:

    #[test]
    fn test_bit_slice_aligned() {
        let buf = Buffer::from(&[2, 4, 6, 8, 10]);
        let slice = buf.bit_slice(8, 8);
        let expected = Buffer::from(&[4]);
        assert_eq!(slice.len(), 1); // actual length is 4!!
        assert_eq!(&slice, &expected);
    }

Expected behavior
Test passes.

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions