Skip to content

Commit f289173

Browse files
committed
Replace EnumVec::push() with try_push().
1 parent cbc4b29 commit f289173

File tree

4 files changed

+96
-84
lines changed

4 files changed

+96
-84
lines changed

src/vec.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<T: EnumArrayHelper<V>, V> EnumVec<T, V> {
3838
{
3939
let mut vec = Self::new();
4040
for key in size.iter() {
41-
vec.push(f(key));
41+
let _ = vec.try_push(f(key));
4242
}
4343
vec
4444
}
@@ -206,14 +206,15 @@ impl<T: EnumArrayHelper<V>, V> EnumVec<T, V> {
206206
}
207207

208208
/// Adds an element to the end of the vector.
209-
///
210-
/// # Panics
211-
/// Panics if the vector is already full.
212-
pub fn push(&mut self, value: V) {
209+
pub fn try_push(&mut self, value: V) -> Result<(), V> {
213210
let len = self.len.as_();
211+
if len >= T::SIZE {
212+
return Err(value);
213+
}
214214
T::partial_slice_mut(&mut self.data)[len] =
215215
mem::MaybeUninit::<V>::new(value);
216216
self.len = self.len.inc();
217+
Ok(())
217218
}
218219

219220
/// Removes an element from the end of the vector and returns it,
@@ -271,8 +272,8 @@ impl<T: EnumArrayHelper<V>, V> Drop for EnumVec<T, V> {
271272
impl<T: EnumArrayHelper<V>, V: Clone> Clone for EnumVec<T, V> {
272273
fn clone(&self) -> Self {
273274
let mut clone = Self::new();
274-
for (_, value) in self.iter() {
275-
clone.push(value.clone())
275+
for value in self.as_slice() {
276+
let _ = clone.try_push(value.clone());
276277
}
277278
clone
278279
}
@@ -328,7 +329,9 @@ impl<T: EnumArrayHelper<V>, V> iter::FromIterator<V> for EnumVec<T, V> {
328329
fn from_iter<I: iter::IntoIterator<Item = V>>(iter: I) -> Self {
329330
let mut c = EnumVec::<T, V>::new();
330331
for i in iter {
331-
c.push(i);
332+
if c.try_push(i).is_err() {
333+
break;
334+
}
332335
}
333336
c
334337
}

tests/test/opt_map.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ fn test_mutable_get() {
430430
// Insert a value and test get_mut
431431
map.insert(Three::A, 100);
432432

433-
let value_mut = map.get_mut(Three::A).unwrap();
433+
let value_mut = map
434+
.get_mut(Three::A)
435+
.expect("Expected get_mut to return Some for existing key");
434436
*value_mut += 50;
435437

436438
assert_eq!(
@@ -440,7 +442,9 @@ fn test_mutable_get() {
440442
);
441443

442444
// Test get_by_index_mut
443-
let value_mut = map.get_by_index_mut(Three::A.into()).unwrap();
445+
let value_mut = map
446+
.get_by_index_mut(Three::A.into())
447+
.expect("Expected get_by_index_mut to return Some for existing key");
444448
*value_mut *= 2;
445449

446450
assert_eq!(

tests/test/serde.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,20 @@ fn test_enum_option_map_empty_round_trip() {
7373

7474
#[test]
7575
fn test_enum_vec_round_trip() {
76-
let mut vec = EnumVec::new();
77-
vec.push(TestData {
78-
value: 10,
79-
name: "first".to_string(),
80-
});
81-
vec.push(TestData {
82-
value: 20,
83-
name: "second".to_string(),
84-
});
85-
vec.push(TestData {
86-
value: 30,
87-
name: "third".to_string(),
88-
});
76+
let vec = EnumVec::from_iter([
77+
TestData {
78+
value: 10,
79+
name: "first".to_string(),
80+
},
81+
TestData {
82+
value: 20,
83+
name: "second".to_string(),
84+
},
85+
TestData {
86+
value: 30,
87+
name: "third".to_string(),
88+
},
89+
]);
8990

9091
let ron_str = ron::to_string(&vec).expect("Serialization failed");
9192
let deserialized: EnumVec<Three, TestData> =

tests/test/vec.rs

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ fn test_push_pop() {
9292
);
9393

9494
// Test push and indexing
95-
vec.push(100);
95+
vec
96+
.try_push(100)
97+
.expect("Failed to push first element to empty vec");
9698
assert!(!vec.is_empty(), "Expected vec to not be empty after push");
9799
assert!(!vec.is_full(), "Expected vec to not be full after one push");
98100
assert_eq!(
@@ -106,7 +108,9 @@ fn test_push_pop() {
106108
"Expected size to reflect one element"
107109
);
108110

109-
vec.push(200);
111+
vec
112+
.try_push(200)
113+
.expect("Failed to push second element to vec");
110114
assert_eq!(
111115
vec[Three::A],
112116
100,
@@ -123,7 +127,9 @@ fn test_push_pop() {
123127
"Expected size to reflect two elements"
124128
);
125129

126-
vec.push(300);
130+
vec
131+
.try_push(300)
132+
.expect("Failed to push third element to vec");
127133
assert!(
128134
vec.is_full(),
129135
"Expected vec to be full after pushing all elements"
@@ -220,9 +226,7 @@ fn test_new_with_constructor() {
220226

221227
#[test]
222228
fn test_get_methods() {
223-
let mut vec = EnumVec::<Three, i32>::new();
224-
vec.push(42);
225-
vec.push(84);
229+
let mut vec: EnumVec<Three, i32> = [42, 84].into_iter().collect();
226230

227231
// Test get and get_by_index for valid indices
228232
assert_eq!(
@@ -258,14 +262,19 @@ fn test_get_methods() {
258262
);
259263

260264
// Test mutable get methods
261-
*vec.get_mut(Three::A).unwrap() = 100;
265+
*vec
266+
.get_mut(Three::A)
267+
.expect("Expected get_mut to return Some for existing element") = 100;
262268
assert_eq!(
263269
vec[Three::A],
264270
100,
265271
"Expected value to be modified through get_mut()"
266272
);
267273

268-
*vec.get_by_index_mut(Three::B.into()).unwrap() = 200;
274+
*vec
275+
.get_by_index_mut(Three::B.into())
276+
.expect("Expected get_by_index_mut to return Some for existing element") =
277+
200;
269278
assert_eq!(
270279
vec[Three::B],
271280
200,
@@ -287,7 +296,7 @@ fn test_get_methods() {
287296

288297
#[test]
289298
fn test_contains() {
290-
let mut vec = EnumVec::<Three, i32>::new();
299+
let vec = EnumVec::<Three, i32>::new();
291300

292301
// Test contains on empty vec
293302
assert!(
@@ -304,48 +313,54 @@ fn test_contains() {
304313
);
305314

306315
// Add elements and test contains
307-
vec.push(10);
308-
assert!(vec.contains(Three::A), "Expected vec to contain Three::A");
316+
let mut vec_with_one: EnumVec<Three, i32> = [10].into_iter().collect();
309317
assert!(
310-
!vec.contains(Three::B),
318+
vec_with_one.contains(Three::A),
319+
"Expected vec to contain Three::A"
320+
);
321+
assert!(
322+
!vec_with_one.contains(Three::B),
311323
"Expected vec to not contain Three::B"
312324
);
313325
assert!(
314-
!vec.contains(Three::C),
326+
!vec_with_one.contains(Three::C),
315327
"Expected vec to not contain Three::C"
316328
);
317329

318-
vec.push(20);
319-
assert!(vec.contains(Three::A), "Expected vec to contain Three::A");
320-
assert!(vec.contains(Three::B), "Expected vec to contain Three::B");
330+
vec_with_one
331+
.try_push(20)
332+
.expect("Failed to push second element in contains test");
321333
assert!(
322-
!vec.contains(Three::C),
334+
vec_with_one.contains(Three::A),
335+
"Expected vec to contain Three::A"
336+
);
337+
assert!(
338+
vec_with_one.contains(Three::B),
339+
"Expected vec to contain Three::B"
340+
);
341+
assert!(
342+
!vec_with_one.contains(Three::C),
323343
"Expected vec to not contain Three::C"
324344
);
325345

326346
// Test contains_index
327347
assert!(
328-
vec.contains_index(Three::A.into()),
348+
vec_with_one.contains_index(Three::A.into()),
329349
"Expected vec to contain index A"
330350
);
331351
assert!(
332-
vec.contains_index(Three::B.into()),
352+
vec_with_one.contains_index(Three::B.into()),
333353
"Expected vec to contain index B"
334354
);
335355
assert!(
336-
!vec.contains_index(Three::C.into()),
356+
!vec_with_one.contains_index(Three::C.into()),
337357
"Expected vec to not contain index C"
338358
);
339359
}
340360

341361
#[test]
342362
fn test_clear() {
343-
let mut vec = EnumVec::<Three, i32>::new();
344-
345-
// Add some elements
346-
vec.push(10);
347-
vec.push(20);
348-
vec.push(30);
363+
let mut vec: EnumVec<Three, i32> = [10, 20, 30].into_iter().collect();
349364

350365
assert!(vec.is_full(), "Expected vec to be full before clear");
351366
assert_eq!(
@@ -380,7 +395,9 @@ fn test_clear() {
380395
);
381396

382397
// Test that we can push again after clear
383-
vec.push(100);
398+
vec
399+
.try_push(100)
400+
.expect("Failed to push element after clear");
384401
assert_eq!(
385402
vec[Three::A],
386403
100,
@@ -395,10 +412,7 @@ fn test_clear() {
395412

396413
#[test]
397414
fn test_remove_at_index() {
398-
let mut vec = EnumVec::<Three, i32>::new();
399-
vec.push(10);
400-
vec.push(20);
401-
vec.push(30);
415+
let mut vec: EnumVec<Three, i32> = [10, 20, 30].into_iter().collect();
402416

403417
// Test remove_at_index returns value for valid index
404418
let removed = vec.remove_at_index(Three::B.into());
@@ -445,10 +459,7 @@ fn test_remove_at_index() {
445459

446460
#[test]
447461
fn test_swap_remove_at_index() {
448-
let mut vec = EnumVec::<Three, i32>::new();
449-
vec.push(10);
450-
vec.push(20);
451-
vec.push(30);
462+
let mut vec: EnumVec<Three, i32> = [10, 20, 30].into_iter().collect();
452463

453464
// Test swap_remove_at_index returns value and replaces with last element
454465
let removed = vec.swap_remove_at_index(Three::A.into());
@@ -495,9 +506,7 @@ fn test_swap_remove_at_index() {
495506

496507
#[test]
497508
fn test_slice_access() {
498-
let mut vec = EnumVec::<Three, i32>::new();
499-
vec.push(10);
500-
vec.push(20);
509+
let mut vec: EnumVec<Three, i32> = [10, 20].into_iter().collect();
501510

502511
// Test as_slice
503512
let slice = vec.as_slice();
@@ -526,15 +535,14 @@ fn test_slice_access() {
526535

527536
#[test]
528537
fn test_iteration_elements_in_order() {
529-
let mut vec = EnumVec::<Three, i32>::new();
538+
let vec = EnumVec::<Three, i32>::new();
530539

531540
// Test iteration on empty vec
532541
let collected: Vec<_> = vec.iter().collect();
533542
assert_eq!(collected, vec![], "Expected empty iteration for empty vec");
534543

535544
// Add elements and test iteration
536-
vec.push(10);
537-
vec.push(20);
545+
let mut vec: EnumVec<Three, i32> = [10, 20].into_iter().collect();
538546

539547
let collected: Vec<_> = vec.iter().collect();
540548
assert_eq!(
@@ -558,10 +566,7 @@ fn test_iteration_elements_in_order() {
558566

559567
#[test]
560568
fn test_iterator_exact_size_and_double_ended() {
561-
let mut vec = EnumVec::<Three, i32>::new();
562-
vec.push(10);
563-
vec.push(20);
564-
vec.push(30);
569+
let vec: EnumVec<Three, i32> = [10, 20, 30].into_iter().collect();
565570

566571
let mut iter = vec.iter();
567572

@@ -623,10 +628,7 @@ fn test_iterator_exact_size_and_double_ended() {
623628

624629
#[test]
625630
fn test_into_iterator() {
626-
let mut vec = EnumVec::<Three, i32>::new();
627-
vec.push(10);
628-
vec.push(20);
629-
vec.push(30);
631+
let mut vec: EnumVec<Three, i32> = [10, 20, 30].into_iter().collect();
630632

631633
// Test IntoIterator for &EnumVec
632634
let collected: Vec<_> = (&vec).into_iter().collect();
@@ -706,10 +708,7 @@ fn test_from_iterator() {
706708

707709
#[test]
708710
fn test_iterator_partial_consumption() {
709-
let mut vec = EnumVec::<Three, i32>::new();
710-
vec.push(10);
711-
vec.push(20);
712-
vec.push(30);
711+
let vec: EnumVec<Three, i32> = [10, 20, 30].into_iter().collect();
713712

714713
let mut iter = vec.iter();
715714

@@ -728,8 +727,7 @@ fn test_iterator_partial_consumption() {
728727

729728
#[test]
730729
fn test_iterator_single_element() {
731-
let mut vec = EnumVec::<Three, i32>::new();
732-
vec.push(42);
730+
let vec: EnumVec<Three, i32> = [42].into_iter().collect();
733731

734732
let collected: Vec<_> = vec.iter().collect();
735733
assert_eq!(
@@ -776,13 +774,19 @@ fn test_remove() {
776774
}
777775

778776
#[test]
779-
#[should_panic(expected = "index out of bounds")]
780-
fn test_push_when_full_panics() {
777+
fn test_push() {
781778
let mut vec = EnumVec::<Three, u16>::new();
782-
vec.push(100);
783-
vec.push(200);
784-
vec.push(300);
785-
vec.push(400);
779+
vec
780+
.try_push(100)
781+
.expect("Failed to push first element in push test");
782+
vec
783+
.try_push(200)
784+
.expect("Failed to push second element in push test");
785+
vec
786+
.try_push(300)
787+
.expect("Failed to push third element in push test");
788+
let overflow = vec.try_push(400);
789+
assert_eq!(overflow, Err(400), "Expected overflow to return the value");
786790
}
787791

788792
#[test]

0 commit comments

Comments
 (0)