From 440bcbf66b82d26e29bd3b801112055edb016aca Mon Sep 17 00:00:00 2001 From: bluss Date: Tue, 22 Dec 2015 08:07:18 +0100 Subject: [PATCH 1/5] Add ArrayVec::dispose() --- src/lib.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ea20055..2dca9de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,9 +57,7 @@ pub struct ArrayVec { impl Drop for ArrayVec { fn drop(&mut self) { - // clear all elements - while let Some(_) = self.pop() { - } + self.clear(); // NoDrop inhibits array's drop // panic safety: NoDrop::drop will trigger on panic, so the inner @@ -340,6 +338,12 @@ impl ArrayVec { } } } + + /// Dispose of `self` without the overwriting that is needed in Drop. + pub fn dispose(mut self) { + self.clear(); + mem::forget(self); + } } impl Deref for ArrayVec { From d991c17a25f13be1165b0301a1fe329a1814066c Mon Sep 17 00:00:00 2001 From: bluss Date: Tue, 22 Dec 2015 08:07:36 +0100 Subject: [PATCH 2/5] ArrayString::push_str: Write capacity check in safer style Use an arithmetic overflow safe conditional. --- src/array_string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array_string.rs b/src/array_string.rs index 154b74c..65a8a02 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -99,7 +99,7 @@ impl> ArrayString { pub fn push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>> { use std::io::Write; - if self.len() + s.len() > self.capacity() { + if s.len() > self.capacity() - self.len() { return Err(CapacityError::new(s)); } unsafe { From 2046e3f7268ae869a20d3db0979ee40a4603489f Mon Sep 17 00:00:00 2001 From: bluss Date: Wed, 13 Jan 2016 12:55:29 +0100 Subject: [PATCH 3/5] Add method CapacityError::simplify This is needed so that CapacityError<&str> isn't prohibitively difficult to use. It converts to CapacityError<()>. --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2dca9de..ad576c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -705,7 +705,7 @@ impl> io::Write for ArrayVec { /// Error value indicating insufficient capacity #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] -pub struct CapacityError { +pub struct CapacityError { element: T, } @@ -720,6 +720,11 @@ impl CapacityError { pub fn element(self) -> T { self.element } + + /// Convert into a `CapacityError` that does not carry an element. + pub fn simplify(self) -> CapacityError { + CapacityError { element: () } + } } const CAPERROR: &'static str = "insufficient capacity"; From 62d372ce1ff2439f3b83385bb5752fe7ac90afa9 Mon Sep 17 00:00:00 2001 From: bluss Date: Wed, 13 Jan 2016 12:59:06 +0100 Subject: [PATCH 4/5] Add DerefMut for ArrayString --- src/array_string.rs | 14 +++++++++++++- tests/tests.rs | 6 ++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/array_string.rs b/src/array_string.rs index 65a8a02..a39fbcc 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -1,7 +1,8 @@ use std::borrow::Borrow; use std::fmt; use std::hash::{Hash, Hasher}; -use std::ops::Deref; +use std::mem; +use std::ops::{Deref, DerefMut}; use std::str; use std::slice; @@ -142,6 +143,17 @@ impl> Deref for ArrayString { } } +impl> DerefMut for ArrayString { + #[inline] + fn deref_mut(&mut self) -> &mut str { + unsafe { + let sl = slice::from_raw_parts_mut(self.xs.as_mut_ptr(), self.len.to_usize()); + // FIXME: Nothing but transmute to do this right now + mem::transmute(sl) + } + } +} + impl> PartialEq for ArrayString { fn eq(&self, rhs: &Self) -> bool { **self == **rhs diff --git a/tests/tests.rs b/tests/tests.rs index 3dd24bd..b0b256d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -283,6 +283,12 @@ fn test_string() { assert!(t.push_str(text).is_err()); assert_eq!(&t, ""); + t.push_str("ab").unwrap(); + // DerefMut + let tmut: &mut str = &mut t; + assert_eq!(tmut, "ab"); + + // Test Error trait / try let t = || -> Result<(), Box> { let mut t = ArrayString::<[_; 2]>::new(); From c9b17628bf28120b9cdcec23e10881c968be3130 Mon Sep 17 00:00:00 2001 From: bluss Date: Wed, 13 Jan 2016 13:06:13 +0100 Subject: [PATCH 5/5] Test on older rust releases in travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index bbfd88c..d1722b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: rust sudo: false matrix: include: + - rust: 1.2.0 - rust: stable - rust: beta - rust: nightly