2020-06-14 00:45:20 -04:00
|
|
|
|
use std::alloc::{alloc, dealloc, handle_alloc_error, realloc, Layout};
|
2020-06-04 16:39:33 -04:00
|
|
|
|
use std::fmt;
|
2020-06-14 00:45:20 -04:00
|
|
|
|
use std::mem::{align_of, ManuallyDrop};
|
2020-06-04 16:39:33 -04:00
|
|
|
|
use std::ops::{Add, AddAssign};
|
2020-06-06 23:40:27 -04:00
|
|
|
|
use std::ptr;
|
|
|
|
|
|
2020-06-06 06:16:08 -04:00
|
|
|
|
/// Buffer for rendered contents
|
|
|
|
|
///
|
|
|
|
|
/// This struct is quite simular to `String`, but some methods are
|
|
|
|
|
/// re-implemented for faster buffering.
|
2020-06-04 16:39:33 -04:00
|
|
|
|
pub struct Buffer {
|
2020-06-06 23:40:27 -04:00
|
|
|
|
data: *mut u8,
|
|
|
|
|
len: usize,
|
|
|
|
|
capacity: usize,
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Buffer {
|
2020-12-16 07:16:39 -05:00
|
|
|
|
/// Create an empty buffer
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
pub const fn new() -> Buffer {
|
|
|
|
|
Self {
|
2020-06-14 00:45:20 -04:00
|
|
|
|
data: align_of::<u8>() as *mut u8, // dangling pointer
|
2020-06-06 23:40:27 -04:00
|
|
|
|
len: 0,
|
|
|
|
|
capacity: 0,
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 04:23:32 -05:00
|
|
|
|
/// Create a empty buffer with a particular capacity
|
2020-12-16 07:16:39 -05:00
|
|
|
|
#[inline]
|
2020-06-04 16:39:33 -04:00
|
|
|
|
pub fn with_capacity(n: usize) -> Buffer {
|
2020-12-16 07:16:39 -05:00
|
|
|
|
if unlikely!(n == 0) {
|
|
|
|
|
Self::new()
|
|
|
|
|
} else {
|
|
|
|
|
Self {
|
|
|
|
|
data: safe_alloc(n),
|
|
|
|
|
len: 0,
|
|
|
|
|
capacity: n,
|
2020-06-06 23:40:27 -04:00
|
|
|
|
}
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 04:23:32 -05:00
|
|
|
|
/// Extracts a string slice containing the entire buffer
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn as_str(&self) -> &str {
|
2020-06-06 23:40:27 -04:00
|
|
|
|
unsafe {
|
|
|
|
|
let bytes = std::slice::from_raw_parts(self.data, self.len);
|
|
|
|
|
std::str::from_utf8_unchecked(bytes)
|
|
|
|
|
}
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 04:23:32 -05:00
|
|
|
|
/// Returns an unsafe mutable pointer to the inner data
|
2020-06-10 05:41:12 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn as_mut_ptr(&self) -> *mut u8 {
|
|
|
|
|
self.data
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 04:23:32 -05:00
|
|
|
|
/// Returns the length of this buffer in bytes
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn len(&self) -> usize {
|
2020-06-06 23:40:27 -04:00
|
|
|
|
self.len
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 04:23:32 -05:00
|
|
|
|
/// Returns this buffer's capacity in bytes
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn capacity(&self) -> usize {
|
2020-06-06 23:40:27 -04:00
|
|
|
|
self.capacity
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 06:49:35 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
pub unsafe fn _set_len(&mut self, new_len: usize) {
|
2020-12-24 01:00:19 -05:00
|
|
|
|
debug_assert!(new_len <= self.capacity);
|
2020-07-15 06:49:35 -04:00
|
|
|
|
self.len = new_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Increase the length of buffer by `additional` bytes
|
2020-06-07 04:58:52 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
2020-07-15 06:49:35 -04:00
|
|
|
|
/// - `additional` must be less than or equal to `capacity() - len()`
|
|
|
|
|
/// - The elements at `old_len..old_len + additional` must be initialized
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
2020-07-15 06:49:35 -04:00
|
|
|
|
pub unsafe fn advance(&mut self, additional: usize) {
|
|
|
|
|
self.len += additional;
|
2020-06-07 04:58:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 04:23:32 -05:00
|
|
|
|
/// Returns `true` if this buffer has a length of zero, and `false` otherwise
|
2020-06-07 04:58:52 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.len == 0
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 07:16:39 -05:00
|
|
|
|
/// Same as String::reserve
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// This method panics if `size` overflows `isize::MAX`.
|
2020-06-16 07:00:35 -04:00
|
|
|
|
#[inline]
|
2020-06-06 23:40:27 -04:00
|
|
|
|
pub fn reserve(&mut self, size: usize) {
|
2020-12-19 07:07:09 -05:00
|
|
|
|
if likely!(size <= self.capacity - self.len) {
|
2020-12-16 20:24:28 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.reserve_internal(size);
|
2020-12-16 00:01:46 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 07:16:39 -05:00
|
|
|
|
/// Same as String::reserve except that undefined behaviour can result if `size`
|
|
|
|
|
/// overflows `isize::MAX`.
|
2020-12-16 00:01:46 -05:00
|
|
|
|
#[inline]
|
|
|
|
|
pub(crate) unsafe fn reserve_small(&mut self, size: usize) {
|
2020-12-16 20:09:18 -05:00
|
|
|
|
debug_assert!(size <= std::isize::MAX as usize);
|
2020-12-19 07:07:09 -05:00
|
|
|
|
if likely!(self.len + size <= self.capacity) {
|
2020-12-16 00:01:46 -05:00
|
|
|
|
return;
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
2020-12-16 00:01:46 -05:00
|
|
|
|
self.reserve_internal(size);
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-11-21 04:43:16 -05:00
|
|
|
|
#[doc(hidden)]
|
2020-06-04 16:39:33 -04:00
|
|
|
|
pub fn clear(&mut self) {
|
2020-06-06 23:40:27 -04:00
|
|
|
|
self.len = 0;
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 07:16:39 -05:00
|
|
|
|
/// Converts a `Buffer` into a `String` without copy/realloc operation.
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn into_string(self) -> String {
|
2020-07-10 13:39:04 -04:00
|
|
|
|
debug_assert!(self.len <= self.capacity);
|
2020-06-14 00:45:20 -04:00
|
|
|
|
let buf = ManuallyDrop::new(self);
|
2020-12-16 07:16:39 -05:00
|
|
|
|
|
|
|
|
|
// SAFETY: This operations satisfy all requirements specified in
|
|
|
|
|
// https://doc.rust-lang.org/std/string/struct.String.html#safety
|
2020-06-14 00:45:20 -04:00
|
|
|
|
unsafe { String::from_raw_parts(buf.data, buf.len, buf.capacity) }
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 04:23:32 -05:00
|
|
|
|
/// Appends a given string slice onto the end of this buffer
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
2020-06-09 09:35:48 -04:00
|
|
|
|
pub fn push_str(&mut self, data: &str) {
|
2020-06-04 16:39:33 -04:00
|
|
|
|
let size = data.len();
|
2020-12-16 20:24:28 -05:00
|
|
|
|
|
2020-06-04 16:39:33 -04:00
|
|
|
|
unsafe {
|
2021-03-28 19:19:13 -04:00
|
|
|
|
// SAFETY: this operation won't overflow because slice cannot exceeds
|
|
|
|
|
// isize::MAX bytes.
|
|
|
|
|
// https://doc.rust-lang.org/reference/behavior-considered-undefined.html
|
|
|
|
|
self.reserve_small(size);
|
|
|
|
|
|
2020-06-06 23:40:27 -04:00
|
|
|
|
let p = self.data.add(self.len);
|
2020-06-04 16:39:33 -04:00
|
|
|
|
std::ptr::copy_nonoverlapping(data.as_ptr(), p, size);
|
2020-06-06 23:40:27 -04:00
|
|
|
|
self.len += size;
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
2020-07-10 13:39:04 -04:00
|
|
|
|
debug_assert!(self.len <= self.capacity);
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 04:23:32 -05:00
|
|
|
|
/// Appends the given `char` to the end of this buffer
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
2020-06-09 09:35:48 -04:00
|
|
|
|
pub fn push(&mut self, data: char) {
|
2020-12-16 07:52:39 -05:00
|
|
|
|
// Question: Is it safe to pass uninitialized memory to `encode_utf8` function?
|
|
|
|
|
unsafe {
|
|
|
|
|
self.reserve_small(4);
|
|
|
|
|
let bp = self.data.add(self.len) as *mut [u8; 4];
|
|
|
|
|
let result = data.encode_utf8(&mut *bp);
|
|
|
|
|
self.len += result.len();
|
|
|
|
|
}
|
2020-06-06 23:40:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 04:27:39 -04:00
|
|
|
|
#[cfg_attr(feature = "perf-inline", inline)]
|
2020-06-16 07:00:35 -04:00
|
|
|
|
fn reserve_internal(&mut self, size: usize) {
|
2020-12-16 20:09:18 -05:00
|
|
|
|
debug_assert!(size <= std::isize::MAX as usize);
|
2020-12-16 07:16:39 -05:00
|
|
|
|
|
2021-03-28 19:19:13 -04:00
|
|
|
|
let new_capacity = std::cmp::max(self.capacity * 2, self.capacity + size);
|
2020-12-16 07:16:39 -05:00
|
|
|
|
debug_assert!(new_capacity > self.capacity);
|
|
|
|
|
self.data = unsafe { safe_realloc(self.data, self.capacity, new_capacity) };
|
|
|
|
|
self.capacity = new_capacity;
|
|
|
|
|
|
2020-07-10 13:39:04 -04:00
|
|
|
|
debug_assert!(!self.data.is_null());
|
|
|
|
|
debug_assert!(self.len <= self.capacity);
|
2020-06-16 07:00:35 -04:00
|
|
|
|
}
|
2020-07-05 00:25:57 -04:00
|
|
|
|
}
|
2020-06-16 07:00:35 -04:00
|
|
|
|
|
2020-12-16 07:16:39 -05:00
|
|
|
|
#[inline(never)]
|
|
|
|
|
fn safe_alloc(capacity: usize) -> *mut u8 {
|
|
|
|
|
assert!(capacity > 0);
|
2020-12-16 20:09:18 -05:00
|
|
|
|
assert!(
|
|
|
|
|
capacity <= std::isize::MAX as usize,
|
|
|
|
|
"capacity is too large"
|
|
|
|
|
);
|
2020-07-21 14:25:07 -04:00
|
|
|
|
|
2020-12-16 07:16:39 -05:00
|
|
|
|
// SAFETY: capacity is non-zero, and always multiple of alignment (1).
|
|
|
|
|
unsafe {
|
|
|
|
|
let layout = Layout::from_size_align_unchecked(capacity, 1);
|
|
|
|
|
let data = alloc(layout);
|
|
|
|
|
if data.is_null() {
|
|
|
|
|
handle_alloc_error(layout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data
|
|
|
|
|
}
|
2020-07-21 14:25:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 07:16:39 -05:00
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// - if `capacity > 0`, `capacity` is the same value that was used to allocate the block
|
|
|
|
|
/// of memory pointed by `ptr`.
|
2020-07-05 00:25:57 -04:00
|
|
|
|
#[cold]
|
2020-12-16 07:16:39 -05:00
|
|
|
|
#[inline(never)]
|
2020-12-16 00:01:46 -05:00
|
|
|
|
unsafe fn safe_realloc(ptr: *mut u8, capacity: usize, new_capacity: usize) -> *mut u8 {
|
2020-12-16 07:16:39 -05:00
|
|
|
|
assert!(new_capacity > 0);
|
2020-12-16 20:09:18 -05:00
|
|
|
|
assert!(
|
|
|
|
|
new_capacity <= std::isize::MAX as usize,
|
|
|
|
|
"capacity is too large"
|
|
|
|
|
);
|
2020-12-16 07:16:39 -05:00
|
|
|
|
|
2020-07-05 00:25:57 -04:00
|
|
|
|
let data = if unlikely!(capacity == 0) {
|
|
|
|
|
let new_layout = Layout::from_size_align_unchecked(new_capacity, 1);
|
|
|
|
|
alloc(new_layout)
|
|
|
|
|
} else {
|
2020-07-09 21:20:10 -04:00
|
|
|
|
let old_layout = Layout::from_size_align_unchecked(capacity, 1);
|
2020-07-05 00:25:57 -04:00
|
|
|
|
realloc(ptr, old_layout, new_capacity)
|
|
|
|
|
};
|
2020-06-18 03:32:07 -04:00
|
|
|
|
|
2020-07-05 00:25:57 -04:00
|
|
|
|
if data.is_null() {
|
|
|
|
|
handle_alloc_error(Layout::from_size_align_unchecked(new_capacity, 1));
|
2020-06-06 23:40:27 -04:00
|
|
|
|
}
|
2020-07-05 00:25:57 -04:00
|
|
|
|
|
|
|
|
|
data
|
2020-06-06 23:40:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 01:52:26 -04:00
|
|
|
|
impl Clone for Buffer {
|
|
|
|
|
fn clone(&self) -> Self {
|
2020-06-12 01:59:47 -04:00
|
|
|
|
unsafe {
|
2020-12-16 07:16:39 -05:00
|
|
|
|
if self.is_empty() {
|
2020-06-12 01:59:47 -04:00
|
|
|
|
Self::new()
|
|
|
|
|
} else {
|
|
|
|
|
let buf = Self {
|
2020-07-21 14:25:07 -04:00
|
|
|
|
data: safe_alloc(self.len),
|
2020-06-12 01:59:47 -04:00
|
|
|
|
len: self.len,
|
|
|
|
|
capacity: self.len,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ptr::copy_nonoverlapping(self.data, buf.data, self.len);
|
|
|
|
|
buf
|
|
|
|
|
}
|
2020-06-12 01:52:26 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for Buffer {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
self.as_str().fmt(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 23:40:27 -04:00
|
|
|
|
impl Drop for Buffer {
|
|
|
|
|
fn drop(&mut self) {
|
2020-06-12 01:54:25 -04:00
|
|
|
|
if self.capacity != 0 {
|
2020-12-16 07:16:39 -05:00
|
|
|
|
// SAFETY: when `self.capacity > 0`, `self.capacity` is the same value
|
|
|
|
|
// used for allocate the block of memory pointed by `self.data`.
|
2020-06-06 23:40:27 -04:00
|
|
|
|
unsafe {
|
2020-06-12 01:54:25 -04:00
|
|
|
|
let layout = Layout::from_size_align_unchecked(self.capacity, 1);
|
|
|
|
|
dealloc(self.data, layout);
|
2020-06-06 23:40:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Write for Buffer {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
2020-06-09 09:35:48 -04:00
|
|
|
|
Buffer::push_str(self, s);
|
2020-06-04 16:39:33 -04:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<String> for Buffer {
|
2020-08-03 14:14:21 -04:00
|
|
|
|
/// Shrink the data and pass raw pointer directory to buffer
|
2020-07-21 18:02:36 -04:00
|
|
|
|
///
|
2020-08-03 14:14:21 -04:00
|
|
|
|
/// This operation is `O(1)`
|
2020-06-04 16:39:33 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
fn from(other: String) -> Buffer {
|
2020-08-03 14:14:21 -04:00
|
|
|
|
let bs = other.into_boxed_str();
|
2020-12-16 07:16:39 -05:00
|
|
|
|
let data = Box::leak(bs);
|
2020-08-03 14:14:21 -04:00
|
|
|
|
Buffer {
|
|
|
|
|
data: data.as_mut_ptr(),
|
|
|
|
|
len: data.len(),
|
|
|
|
|
capacity: data.len(),
|
|
|
|
|
}
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<&str> for Buffer {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn from(other: &str) -> Buffer {
|
2020-07-21 18:02:36 -04:00
|
|
|
|
let mut buf = Buffer::with_capacity(other.len());
|
2020-12-16 07:16:39 -05:00
|
|
|
|
|
|
|
|
|
if !other.is_empty() {
|
|
|
|
|
// SAFETY: `Buffer.capacity()` should be same as `other.len()`, so if `other`
|
|
|
|
|
// is not empty, `buf.as_mut_ptr()` is supporsed to point to valid memory.
|
|
|
|
|
unsafe {
|
|
|
|
|
ptr::copy_nonoverlapping(other.as_ptr(), buf.as_mut_ptr(), other.len());
|
|
|
|
|
buf.advance(other.len());
|
|
|
|
|
}
|
2020-07-21 18:02:36 -04:00
|
|
|
|
}
|
2020-12-16 07:16:39 -05:00
|
|
|
|
|
2020-07-21 18:02:36 -04:00
|
|
|
|
buf
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Add<&str> for Buffer {
|
|
|
|
|
type Output = Buffer;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn add(mut self, other: &str) -> Buffer {
|
2020-06-09 09:35:48 -04:00
|
|
|
|
self.push_str(other);
|
2020-06-04 16:39:33 -04:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AddAssign<&str> for Buffer {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn add_assign(&mut self, other: &str) {
|
2020-06-09 09:35:48 -04:00
|
|
|
|
self.push_str(other)
|
2020-06-04 16:39:33 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Buffer {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn default() -> Buffer {
|
|
|
|
|
Buffer::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-06 23:40:27 -04:00
|
|
|
|
|
2020-12-19 04:21:48 -05:00
|
|
|
|
unsafe impl Send for Buffer {}
|
|
|
|
|
unsafe impl Sync for Buffer {}
|
|
|
|
|
|
2020-06-06 23:40:27 -04:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2020-12-28 11:32:56 -05:00
|
|
|
|
use super::*;
|
|
|
|
|
use std::sync::{Arc, Barrier, Mutex};
|
|
|
|
|
use std::thread;
|
2020-06-06 23:40:27 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
2020-12-17 02:30:47 -05:00
|
|
|
|
fn push_str() {
|
2020-06-06 23:40:27 -04:00
|
|
|
|
let mut buffer = Buffer::new();
|
2020-06-11 06:02:28 -04:00
|
|
|
|
assert_eq!(buffer.len(), 0);
|
|
|
|
|
assert_eq!(buffer.capacity(), 0);
|
2020-06-06 23:40:27 -04:00
|
|
|
|
|
2020-06-09 09:35:48 -04:00
|
|
|
|
buffer.push_str("apple");
|
2020-06-11 06:02:28 -04:00
|
|
|
|
assert_eq!(buffer.len(), 5);
|
|
|
|
|
assert_eq!(buffer.capacity(), 5);
|
2020-06-06 23:40:27 -04:00
|
|
|
|
|
2020-06-09 09:35:48 -04:00
|
|
|
|
buffer.push_str("pie");
|
2020-06-11 06:02:28 -04:00
|
|
|
|
assert_eq!(buffer.len(), 8);
|
|
|
|
|
assert_eq!(buffer.capacity(), 10);
|
2020-12-28 11:32:56 -05:00
|
|
|
|
|
|
|
|
|
for _ in 0..16 {
|
|
|
|
|
buffer.push_str("zomg");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert_eq!(buffer.len(), 72);
|
|
|
|
|
assert_eq!(buffer.capacity(), 80);
|
2020-06-11 06:02:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2020-12-17 02:30:47 -05:00
|
|
|
|
fn with_capacity() {
|
|
|
|
|
let buffer = Buffer::with_capacity(1);
|
2020-06-11 06:02:28 -04:00
|
|
|
|
assert!(buffer.is_empty());
|
|
|
|
|
assert_eq!(buffer.len(), 0);
|
|
|
|
|
assert!(buffer.capacity() >= 1);
|
2020-06-06 23:40:27 -04:00
|
|
|
|
}
|
2020-06-06 23:57:15 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn string_conversion() {
|
|
|
|
|
// from empty string
|
2020-12-28 11:32:56 -05:00
|
|
|
|
let s = String::with_capacity(2);
|
|
|
|
|
assert!(s.capacity() >= 2);
|
|
|
|
|
|
2020-06-06 23:57:15 -04:00
|
|
|
|
let mut buf = Buffer::from(s);
|
|
|
|
|
assert_eq!(buf.as_str(), "");
|
2020-12-28 11:32:56 -05:00
|
|
|
|
|
|
|
|
|
// capacity should be shrinked for safety
|
|
|
|
|
assert_eq!(buf.capacity(), 0);
|
|
|
|
|
|
2020-06-09 09:35:48 -04:00
|
|
|
|
buf.push_str("abc");
|
2020-06-06 23:57:15 -04:00
|
|
|
|
assert_eq!(buf.as_str(), "abc");
|
|
|
|
|
|
|
|
|
|
// into non-empty string
|
|
|
|
|
let mut s = buf.into_string();
|
|
|
|
|
assert_eq!(s, "abc");
|
|
|
|
|
|
2020-12-28 14:51:08 -05:00
|
|
|
|
s += "defghijklmn";
|
2020-06-06 23:57:15 -04:00
|
|
|
|
assert_eq!(s, "abcdefghijklmn");
|
|
|
|
|
|
|
|
|
|
// from non-empty string
|
|
|
|
|
let mut buf = Buffer::from(s);
|
|
|
|
|
assert_eq!(buf.as_str(), "abcdefghijklmn");
|
|
|
|
|
buf.clear();
|
|
|
|
|
assert_eq!(buf.as_str(), "");
|
|
|
|
|
|
|
|
|
|
// into empty string
|
2020-07-14 05:27:19 -04:00
|
|
|
|
let buf = Buffer::default();
|
2020-06-06 23:57:15 -04:00
|
|
|
|
let mut s = buf.into_string();
|
|
|
|
|
assert_eq!(s, "");
|
|
|
|
|
|
|
|
|
|
s.push_str("apple");
|
|
|
|
|
assert_eq!(s, "apple");
|
|
|
|
|
}
|
2020-07-14 05:27:19 -04:00
|
|
|
|
|
2020-12-28 11:32:56 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn from_str() {
|
|
|
|
|
let buf = Buffer::from("abcdefgh");
|
|
|
|
|
assert_eq!(buf.as_str(), "abcdefgh");
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 05:27:19 -04:00
|
|
|
|
#[test]
|
|
|
|
|
fn clone() {
|
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
|
|
let mut s1 = Buffer::with_capacity(0);
|
|
|
|
|
let mut s2 = s1.clone();
|
|
|
|
|
|
|
|
|
|
s1.push('a');
|
|
|
|
|
s2.push_str("b");
|
|
|
|
|
|
|
|
|
|
assert_eq!(s1.as_str(), "a");
|
|
|
|
|
assert_eq!(s2.as_str(), "b");
|
|
|
|
|
|
|
|
|
|
let mut s1 = Buffer::from("foo");
|
|
|
|
|
let mut s2 = s1.clone();
|
|
|
|
|
|
|
|
|
|
s1 = s1 + "bar";
|
|
|
|
|
write!(s2, "baz").unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(s1.as_str(), "foobar");
|
|
|
|
|
assert_eq!(s2.as_str(), "foobaz");
|
2020-12-16 07:16:39 -05:00
|
|
|
|
|
|
|
|
|
s2.clear();
|
|
|
|
|
let _ = s2.clone();
|
2020-07-14 05:27:19 -04:00
|
|
|
|
}
|
2020-12-16 07:52:39 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn push() {
|
|
|
|
|
for initial_capacity in &[0, 4, 16] {
|
|
|
|
|
let mut s = Buffer::with_capacity(*initial_capacity);
|
|
|
|
|
|
|
|
|
|
s.push('a');
|
|
|
|
|
s.push('é');
|
|
|
|
|
s.push('A');
|
|
|
|
|
s.push('🄫');
|
|
|
|
|
|
|
|
|
|
assert_eq!(s.as_str(), "aéA🄫");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-28 11:32:56 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn multi_thread() {
|
|
|
|
|
const THREADS: usize = 8;
|
|
|
|
|
const ITERS: usize = 100;
|
|
|
|
|
|
|
|
|
|
let barrier = Arc::new(Barrier::new(THREADS));
|
|
|
|
|
let buffer = Arc::new(Mutex::new(Buffer::new()));
|
|
|
|
|
let mut handles = Vec::with_capacity(THREADS);
|
|
|
|
|
|
|
|
|
|
for _ in 0..THREADS {
|
|
|
|
|
let barrier = barrier.clone();
|
|
|
|
|
let buffer = buffer.clone();
|
|
|
|
|
|
|
|
|
|
handles.push(thread::spawn(move || {
|
|
|
|
|
barrier.wait();
|
|
|
|
|
for _ in 0..ITERS {
|
|
|
|
|
buffer.lock().unwrap().push_str("a");
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for handle in handles {
|
|
|
|
|
handle.join().unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert_eq!(buffer.lock().unwrap().as_str(), "a".repeat(ITERS * THREADS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn reserve_overflow() {
|
|
|
|
|
let mut buf = Buffer::new();
|
|
|
|
|
buf.reserve(std::isize::MAX as usize + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn empty_alloc() {
|
|
|
|
|
safe_alloc(0);
|
|
|
|
|
}
|
2020-06-06 23:40:27 -04:00
|
|
|
|
}
|