Skip to content

Commit 267b65e

Browse files
Use generic InsufficientCapacity error type
"InsufficientCapacity" is a more precise description than "VectorFull" or similar error names.
1 parent 340c7b6 commit 267b65e

File tree

4 files changed

+37
-55
lines changed

4 files changed

+37
-55
lines changed

src/containers/generic/queue.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
// *******************************************************************************
1313

14-
use core::fmt;
1514
use core::marker::PhantomData;
1615
use core::mem::needs_drop;
1716
use core::ops::Range;
1817
use core::ptr;
1918

19+
use crate::InsufficientCapacity;
2020
use crate::storage::Storage;
2121

2222
#[repr(C)]
@@ -104,8 +104,8 @@ impl<T, S: Storage<T>> GenericQueue<T, S> {
104104
/// Tries to push an element to the back of the queue.
105105
///
106106
/// If the queue has spare capacity, the push succeeds and a reference to that element
107-
/// is returned; otherwise, `Err(QueueFull)` is returned.
108-
pub fn push_back(&mut self, value: T) -> Result<&mut T, QueueFull> {
107+
/// is returned; otherwise, `Err(InsufficientCapacity)` is returned.
108+
pub fn push_back(&mut self, value: T) -> Result<&mut T, InsufficientCapacity> {
109109
let capacity = self.storage.capacity();
110110
if self.len < capacity {
111111
let write_pos = self.front_index as u64 + self.len as u64;
@@ -117,15 +117,15 @@ impl<T, S: Storage<T>> GenericQueue<T, S> {
117117
self.len += 1;
118118
Ok(unsafe { self.storage.element_mut(write_pos).write(value) })
119119
} else {
120-
Err(QueueFull)
120+
Err(InsufficientCapacity)
121121
}
122122
}
123123

124124
/// Tries to push an element to the front of the queue.
125125
///
126126
/// If the queue has spare capacity, the push succeeds and a reference to that element
127-
/// is returned; otherwise, `Err(QueueFull)` is returned.
128-
pub fn push_front(&mut self, value: T) -> Result<&mut T, QueueFull> {
127+
/// is returned; otherwise, `Err(InsufficientCapacity)` is returned.
128+
pub fn push_front(&mut self, value: T) -> Result<&mut T, InsufficientCapacity> {
129129
let capacity = self.storage.capacity();
130130
if self.len < capacity {
131131
let write_pos = if self.front_index > 0 { self.front_index - 1 } else { capacity - 1 };
@@ -134,7 +134,7 @@ impl<T, S: Storage<T>> GenericQueue<T, S> {
134134
self.front_index = write_pos;
135135
Ok(element)
136136
} else {
137-
Err(QueueFull)
137+
Err(InsufficientCapacity)
138138
}
139139
}
140140

@@ -203,18 +203,6 @@ impl<T, S: Storage<T>> GenericQueue<T, S> {
203203
}
204204
}
205205

206-
/// Indicates that an operation failed because the queue would exceed its maximum capacity.
207-
#[derive(Clone, Copy, Default, Debug)]
208-
pub struct QueueFull;
209-
210-
impl fmt::Display for QueueFull {
211-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212-
write!(f, "queue is full")
213-
}
214-
}
215-
216-
impl core::error::Error for QueueFull {}
217-
218206
#[cfg(test)]
219207
mod tests {
220208
use std::{collections::VecDeque, mem::MaybeUninit};

src/containers/generic/string.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use core::ops;
1616
use core::str;
1717

1818
use super::vec::GenericVec;
19+
use crate::InsufficientCapacity;
1920
use crate::storage::Storage;
2021

2122
#[repr(transparent)]
@@ -86,23 +87,23 @@ impl<S: Storage<u8>> GenericString<S> {
8687
/// Tries to append the given character to the end of the string.
8788
///
8889
/// If the string has sufficient spare capacity, the operation succeeds and a reference to the new character is returned;
89-
/// otherwise, `Err(StringFull)` is returned.
90-
pub fn push(&mut self, ch: char) -> Result<&mut str, StringFull> {
90+
/// otherwise, `Err(InsufficientCapacity)` is returned.
91+
pub fn push(&mut self, ch: char) -> Result<&mut str, InsufficientCapacity> {
9192
let mut buffer = [0_u8; 4];
9293
self.push_str(ch.encode_utf8(&mut buffer))
9394
}
9495

9596
/// Tries to append the given string slice to the end of the string.
9697
///
9798
/// If the string has sufficient spare capacity, the operation succeeds and a reference to the new characters is returned;
98-
/// otherwise, `Err(StringFull)` is returned.
99-
pub fn push_str(&mut self, other: &str) -> Result<&mut str, StringFull> {
99+
/// otherwise, `Err(InsufficientCapacity)` is returned.
100+
pub fn push_str(&mut self, other: &str) -> Result<&mut str, InsufficientCapacity> {
100101
match self.vec.extend_from_slice(other.as_bytes()) {
101102
Ok(bytes) => Ok(
102103
// SAFETY: `bytes == string.as_bytes()`, therefore this is valid UTF-8
103104
unsafe { str::from_utf8_unchecked_mut(bytes) },
104105
),
105-
Err(_) => Err(StringFull),
106+
Err(_) => Err(InsufficientCapacity),
106107
}
107108
}
108109

@@ -147,18 +148,6 @@ impl<S: Storage<u8>> fmt::Debug for GenericString<S> {
147148
}
148149
}
149150

150-
/// Indicates that an operation failed because the string would exceed its maximum capacity.
151-
#[derive(Clone, Copy, Default, Debug)]
152-
pub struct StringFull;
153-
154-
impl fmt::Display for StringFull {
155-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156-
write!(f, "string is full")
157-
}
158-
}
159-
160-
impl core::error::Error for StringFull {}
161-
162151
#[cfg(test)]
163152
mod tests {
164153
use std::mem::MaybeUninit;

src/containers/generic/vec.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use core::mem::needs_drop;
1717
use core::ops;
1818
use core::ptr;
1919

20+
use crate::InsufficientCapacity;
2021
use crate::storage::Storage;
2122

2223
#[repr(C)]
@@ -88,14 +89,14 @@ impl<T, S: Storage<T>> GenericVec<T, S> {
8889
/// Tries to push an element to the back of the vector.
8990
///
9091
/// If the vector has spare capacity, the push succeeds and a reference to that element
91-
/// is returned; otherwise, `Err(VectorFull)` is returned.
92-
pub fn push(&mut self, value: T) -> Result<&mut T, VectorFull> {
92+
/// is returned; otherwise, `Err(InsufficientCapacity)` is returned.
93+
pub fn push(&mut self, value: T) -> Result<&mut T, InsufficientCapacity> {
9394
if self.len < self.storage.capacity() {
9495
let element = unsafe { self.storage.element_mut(self.len) }.write(value);
9596
self.len += 1;
9697
Ok(element)
9798
} else {
98-
Err(VectorFull)
99+
Err(InsufficientCapacity)
99100
}
100101
}
101102

@@ -140,9 +141,9 @@ impl<T: Copy, S: Storage<T>> GenericVec<T, S> {
140141
/// Tries to append a copy of the given slice to the end of the vector.
141142
///
142143
/// If the vector has sufficient spare capacity, the operation succeeds and a reference to those elements is returned;
143-
/// otherwise, `Err(VectorFull)` is returned.
144-
pub fn extend_from_slice(&mut self, other: &[T]) -> Result<&mut [T], VectorFull> {
145-
let new_len = (self.len as usize).checked_add(other.len()).ok_or(VectorFull)?;
144+
/// otherwise, `Err(InsufficientCapacity)` is returned.
145+
pub fn extend_from_slice(&mut self, other: &[T]) -> Result<&mut [T], InsufficientCapacity> {
146+
let new_len = (self.len as usize).checked_add(other.len()).ok_or(InsufficientCapacity)?;
146147
if new_len <= self.capacity() {
147148
let new_len = new_len as u32; // No overflow, because new_len <= capacity <= u32::MAX
148149
// SAFETY:
@@ -161,7 +162,7 @@ impl<T: Copy, S: Storage<T>> GenericVec<T, S> {
161162
// SAFETY: the memory in the `target` slice has now been initialized
162163
Ok(unsafe { &mut *target })
163164
} else {
164-
Err(VectorFull)
165+
Err(InsufficientCapacity)
165166
}
166167
}
167168
}
@@ -186,18 +187,6 @@ impl<T: fmt::Debug, S: Storage<T>> fmt::Debug for GenericVec<T, S> {
186187
}
187188
}
188189

189-
/// Indicates that an operation failed because the vector would exceed its maximum capacity.
190-
#[derive(Clone, Copy, Default, Debug)]
191-
pub struct VectorFull;
192-
193-
impl fmt::Display for VectorFull {
194-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
195-
write!(f, "vector is full")
196-
}
197-
}
198-
199-
impl core::error::Error for VectorFull {}
200-
201190
#[cfg(test)]
202191
mod tests {
203192
use std::mem::MaybeUninit;

src/containers/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ pub mod fixed_capacity;
1919
pub(crate) mod generic;
2020
pub mod inline;
2121
pub(crate) mod storage;
22+
23+
use core::fmt;
24+
25+
/// Indicates that an operation failed because the container doesn't have enough remaining capacity.
26+
///
27+
/// Note that this doesn't necessarily mean that the container is full.
28+
#[derive(Clone, Copy, Default, Debug)]
29+
pub struct InsufficientCapacity;
30+
31+
impl fmt::Display for InsufficientCapacity {
32+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33+
write!(f, "insufficient capacity for this operation")
34+
}
35+
}
36+
37+
impl core::error::Error for InsufficientCapacity {}

0 commit comments

Comments
 (0)