#slice #move #index #chunk

no-std moveslice

一个用于在切片中移动块的单函数库

6 个稳定版本

2.0.1 2019 年 8 月 3 日
1.1.0 2019 年 8 月 3 日
1.0.3 2019 年 8 月 2 日

#2579 in Rust 模式

Download history 20/week @ 2024-03-14 18/week @ 2024-03-21 33/week @ 2024-03-28 21/week @ 2024-04-04 23/week @ 2024-04-11 34/week @ 2024-04-18 25/week @ 2024-04-25 15/week @ 2024-05-02 21/week @ 2024-05-09 21/week @ 2024-05-16 19/week @ 2024-05-23 17/week @ 2024-05-30 13/week @ 2024-06-06 15/week @ 2024-06-13 24/week @ 2024-06-20 10/week @ 2024-06-27

每月 65 次下载
6 个crate中 使用

MPL-2.0 许可证

11KB
77

moveslice

moveslice moveslice

此crate包含在数组内部移动切片的功能。它仅使用安全函数,并通过使用split_at_mutrotate_left/rotate_right函数来高效地工作。

此crate还侧重于实现no_std,以便在所有需要此功能的情况下使用。

此crate提供的主要功能是为所有切片/数组实现moveslice函数。实际上,它针对实现了AsMut<[T]> trait的任何类型都实现了它。这包括切片和向量。

示例

use moveslice::{Moveslice, Error};

let mut arr = [1,2,3,4,5,6,7,8,9];

// The following moves the slice 3..6 to index 1.
// In effect, it moves [4,5,6] over to where [2] is.
arr.moveslice(3..6, 1);
assert_eq!(arr, [1,4,5,6,2,3,7,8,9]);

// The following moves the slice 3..6 to index 6.
// In effect, it moves [6,2,3] over to where [7] is.
arr.moveslice(3..6, 6);
assert_eq!(arr, [1,4,5,7,8,9,6,2,3]);

// The following attempts to move the slice beyond boundaries.
// The index given is 7, which exists in the array, but the
// last element of the chunk will not fit (7 + 3 = 10 > 9).
// Therefore, the following should fail.
arr.moveslice(3..6, 7); // will panic

// Panicking on failure however can prove to be not ideal.
// If instead of panicking, you prefer a `Result`, use
// `try_moveslice`.
let res = arr.try_moveslice(3..6, 7);
assert!(res.is_err());

// Moveslice also comes with its own `Error` enum, with diagnostic
// information to help debugging. The line before would have triggered
// an OutOfBoundsMove error. The following line would trigger the
// InvalidBounds error.
let res = arr.try_moveslice(9..10, 7);
assert!(if let Err(Error::InvalidBounds{..}) = res {true} else {false});

// You could pass the destination as the same value as chunk.0.
// However this would mean nothing is moved.
// This doesn't panic, but it's a no-op.
arr.moveslice(0..3, 0);

许可证:MPL-2.0

无运行时依赖