Skip to content

Commit 2bc8ed1

Browse files
Implement append (#37)
1 parent f7b0f86 commit 2bc8ed1

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

.lefthook.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pre-commit:
22
commands:
33
quality:
4-
run: make
4+
run: make deny-commit-on-master && make

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ dev:
5050

5151
echo Installing cargo-fuzz...
5252
cargo install cargo-fuzz
53+
54+
deny-commit-on-master:
55+
ifeq ($(shell git symbolic-ref --short HEAD),master)
56+
$(error Direct commits to 'master' are not allowed.)
57+
endif

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ All operations are O(1) except:
3434
| `clear` | O(current length) | O(1) |
3535
| `set_len` | O(new length - current length) | O(new length - current length) |
3636
| `extend_from_slice` | O(slice length) | O(slice length) |
37+
| `append` | O(other vector length) | O(other vector length) |
3738

3839
## Add to project
3940

src/lib.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,45 @@ impl<T, const CAPACITY: usize> Vec<T, CAPACITY> {
637637
Ok(())
638638
}
639639

640+
/// Moves elements of `other` vector at the end of the current vector. `other` will be empty.
641+
///
642+
/// # Errors
643+
///
644+
/// Returns [`CapacityError`] if adding elements from `other` would result in current vector exceeding its capacity.
645+
///
646+
/// # Example
647+
///
648+
/// ```rust
649+
/// use static_vector::Vec;
650+
///
651+
/// let mut vec = Vec::<i32, 10>::new();
652+
/// vec.push(1).unwrap();
653+
/// vec.push(2).unwrap();
654+
/// vec.push(3).unwrap();
655+
///
656+
/// let mut other = Vec::<i32, 20>::new();
657+
/// other.push(4).unwrap();
658+
/// other.push(5).unwrap();
659+
/// other.push(6).unwrap();
660+
///
661+
/// vec.append(&mut other).unwrap();
662+
/// assert_eq!(vec.as_slice(), [1, 2, 3, 4, 5, 6]);
663+
/// assert_eq!(other.as_slice(), []);
664+
/// ```
665+
#[inline]
666+
pub fn append<const OTHER_CAPACITY: usize>(
667+
&mut self,
668+
other: &mut Vec<T, OTHER_CAPACITY>,
669+
) -> Result<(), CapacityError>
670+
where
671+
T: Clone,
672+
{
673+
self.extend_from_slice(other.as_slice())?;
674+
other.clear();
675+
676+
Ok(())
677+
}
678+
640679
/// Adds the given `value` to the end of the vector without checking bounds.
641680
/// For internal and controlled use only.
642681
fn push_unchecked(&mut self, value: T) {
@@ -1397,6 +1436,44 @@ mod tests {
13971436
assert_eq!(dst.as_slice(), [1, 2, 3]);
13981437
}
13991438

1439+
#[test]
1440+
fn append_with_enough_room() {
1441+
let mut vec = Vec::<i32, 5>::new();
1442+
vec.push(1).unwrap();
1443+
vec.push(2).unwrap();
1444+
1445+
let mut other = Vec::<i32, 20>::new();
1446+
other.push(3).unwrap();
1447+
other.push(4).unwrap();
1448+
1449+
let result = vec.append(&mut other);
1450+
1451+
assert!(result.is_ok());
1452+
assert_eq!(vec.len(), 4);
1453+
assert_eq!(vec.as_slice(), [1, 2, 3, 4]);
1454+
assert!(other.is_empty());
1455+
assert_eq!(other.as_slice(), []);
1456+
}
1457+
1458+
#[test]
1459+
fn append_with_not_enough_room() {
1460+
let mut vec = Vec::<i32, 2>::new();
1461+
vec.push(1).unwrap();
1462+
vec.push(2).unwrap();
1463+
1464+
let mut other = Vec::<i32, 20>::new();
1465+
other.push(3).unwrap();
1466+
other.push(4).unwrap();
1467+
1468+
let result = vec.append(&mut other);
1469+
1470+
assert!(result.is_err());
1471+
assert_eq!(vec.len(), 2);
1472+
assert_eq!(vec.as_slice(), [1, 2]);
1473+
assert_eq!(other.len(), 2);
1474+
assert_eq!(other.as_slice(), [3, 4]);
1475+
}
1476+
14001477
#[test]
14011478
fn clone() {
14021479
let mut vec = Vec::<i32, 5>::new();

0 commit comments

Comments
 (0)