[][src]Function str_concat::concat_slice

pub unsafe fn concat_slice<'a, T>(
    a: &'a [T],
    b: &'a [T]
) -> Result<&'a [T], Error>

Concatenate two slices if they are adjacent.

If two slices are adjacent to each other in memory, this function concatenates both, creating a single longer slice. Note that slices of zero-sized types (ZST) are never considered adjacent. Otherwise it would be possible to concatenate a slice to itself.

Errors

Returns Err if the two slices aren't adjacent, a is after b, or if the result is too long to be represented as a slice (size in bytes is larger than isize::MAX).

When T is a zero-sized type (ZST) then always returns Err(NotAdjacent) otherwise. This is because ZST-slices are extra weird and their safety is not yet fully determined.

Safety

The provided slices must come from the same underlying allocation. The adjacency test can not reliably differentiate between the one-past-the-end pointer of one allocation and the start of another. However, all slices must be within a single allocation.

Examples

Correct usage:

let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
unsafe {
    // SAFETY: slices from the same bytes originally.
    assert_eq!(
        [0, 1, 2, 3, 4, 5, 6], 
        concat_slice(&s[..5], &s[5..7]).unwrap());
}

Non-adjacent byte slices:

let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
unsafe {
    // SAFETY: slices from the same bytes originally.
    assert_eq!(Err(Error::NotAdjacent), concat_slice(&s[..5], &s[6..7]))
}