Use no public fields for CapacityError

This commit is contained in:
bluss
2015-09-18 01:00:55 +02:00
parent 90de29e6cb
commit 7b47f1e891
2 changed files with 10 additions and 6 deletions
+3 -3
View File
@@ -70,7 +70,7 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// let overflow = string.push('c'); /// let overflow = string.push('c');
/// ///
/// assert_eq!(&string[..], "ab"); /// assert_eq!(&string[..], "ab");
/// assert_eq!(overflow.err().map(|e| e.element), Some('c')); /// assert_eq!(overflow.unwrap_err().element(), 'c');
/// ``` /// ```
pub fn push(&mut self, c: char) -> Result<(), CapacityError<char>> { pub fn push(&mut self, c: char) -> Result<(), CapacityError<char>> {
use std::fmt::Write; use std::fmt::Write;
@@ -93,8 +93,8 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// let overflow2 = string.push_str("ef"); /// let overflow2 = string.push_str("ef");
/// ///
/// assert_eq!(&string[..], "ad"); /// assert_eq!(&string[..], "ad");
/// assert_eq!(overflow1.err().map(|e| e.element), Some("bc")); /// assert_eq!(overflow1.unwrap_err().element(), "bc");
/// assert_eq!(overflow2.err().map(|e| e.element), Some("ef")); /// assert_eq!(overflow2.unwrap_err().element(), "ef");
/// ``` /// ```
pub fn push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>> { pub fn push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>> {
use std::io::Write; use std::io::Write;
+7 -3
View File
@@ -36,19 +36,23 @@ unsafe fn new_array<A: Array>() -> A {
mem::uninitialized() mem::uninitialized()
} }
/// Error value indicating insufficient capacity
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct CapacityError<T> { pub struct CapacityError<T> {
_unused: (), element: T,
pub element: T,
} }
impl<T> CapacityError<T> { impl<T> CapacityError<T> {
fn new(element: T) -> CapacityError<T> { fn new(element: T) -> CapacityError<T> {
CapacityError { CapacityError {
_unused: (),
element: element, element: element,
} }
} }
/// Extract the overflowing element
pub fn element(self) -> T {
self.element
}
} }
/// A vector with a fixed capacity. /// A vector with a fixed capacity.