Implement Error trait for CapacityError
This commit is contained in:
+41
-19
@@ -15,7 +15,9 @@ use std::slice;
|
|||||||
use nodrop::NoDrop;
|
use nodrop::NoDrop;
|
||||||
|
|
||||||
// extra traits
|
// extra traits
|
||||||
|
use std::any::Any;
|
||||||
use std::borrow::{Borrow, BorrowMut};
|
use std::borrow::{Borrow, BorrowMut};
|
||||||
|
use std::error::Error;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
@@ -36,25 +38,6 @@ unsafe fn new_array<A: Array>() -> A {
|
|||||||
mem::uninitialized()
|
mem::uninitialized()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error value indicating insufficient capacity
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
|
|
||||||
pub struct CapacityError<T> {
|
|
||||||
element: T,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> CapacityError<T> {
|
|
||||||
fn new(element: T) -> CapacityError<T> {
|
|
||||||
CapacityError {
|
|
||||||
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.
|
||||||
///
|
///
|
||||||
/// The `ArrayVec` is a vector backed by a fixed size array. It keeps track of
|
/// The `ArrayVec` is a vector backed by a fixed size array. It keeps track of
|
||||||
@@ -715,3 +698,42 @@ impl<A: Array<Item=u8>> io::Write for ArrayVec<A> {
|
|||||||
}
|
}
|
||||||
fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Error value indicating insufficient capacity
|
||||||
|
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
|
||||||
|
pub struct CapacityError<T> {
|
||||||
|
element: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> CapacityError<T> {
|
||||||
|
fn new(element: T) -> CapacityError<T> {
|
||||||
|
CapacityError {
|
||||||
|
element: element,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract the overflowing element
|
||||||
|
pub fn element(self) -> T {
|
||||||
|
self.element
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const CAPERROR: &'static str = "insufficient capacity";
|
||||||
|
|
||||||
|
impl<T: Any> Error for CapacityError<T> {
|
||||||
|
fn description(&self) -> &str {
|
||||||
|
CAPERROR
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> fmt::Display for CapacityError<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", CAPERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> fmt::Debug for CapacityError<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}: {}", "CapacityError", CAPERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -266,6 +266,8 @@ fn test_write() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_string() {
|
fn test_string() {
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
let text = "hello world";
|
let text = "hello world";
|
||||||
let mut s = ArrayString::<[_; 16]>::new();
|
let mut s = ArrayString::<[_; 16]>::new();
|
||||||
s.push_str(text).unwrap();
|
s.push_str(text).unwrap();
|
||||||
@@ -280,4 +282,12 @@ fn test_string() {
|
|||||||
let mut t = ArrayString::<[_; 2]>::new();
|
let mut t = ArrayString::<[_; 2]>::new();
|
||||||
assert!(t.push_str(text).is_err());
|
assert!(t.push_str(text).is_err());
|
||||||
assert_eq!(&t, "");
|
assert_eq!(&t, "");
|
||||||
|
|
||||||
|
// Test Error trait / try
|
||||||
|
let t = || -> Result<(), Box<Error>> {
|
||||||
|
let mut t = ArrayString::<[_; 2]>::new();
|
||||||
|
try!(t.push_str(text));
|
||||||
|
Ok(())
|
||||||
|
}();
|
||||||
|
assert!(t.is_err());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user