1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
#![doc = include_str!("../README.md")]
#![deny(unsafe_op_in_unsafe_fn)]
mod raw;
use std::fmt;
use std::ops::Index;
/// Creates a [`Vec`] containing the given elements.
///
/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
/// There are two forms of this macro:
///
/// - Create a [`Vec`] containing a given list of elements:
///
/// ```
/// let vec = vec![1, 2, 3];
/// assert_eq!(vec[0], 1);
/// assert_eq!(vec[1], 2);
/// assert_eq!(vec[2], 3);
/// ```
///
/// - Create a [`Vec`] from a given element and size:
///
/// ```
/// let vec = vec![1; 3];
/// assert_eq!(vec, [1, 1, 1]);
/// ```
#[macro_export]
macro_rules! vec {
() => {
$crate::Vec::new()
};
($elem:expr; $n:expr) => {{
let vec = $crate::Vec::with_capacity($n);
vec.extend(::core::iter::repeat($elem).take($n));
vec
}};
($($x:expr),+ $(,)?) => (
<$crate::Vec<_> as core::iter::FromIterator<_>>::from_iter([$($x),+])
);
}
/// A concurrent, append-only vector.
///
/// See [the crate documentation](crate) for details.
///
/// # Notes
///
/// The bucket array is stored inline, meaning that the
/// `Vec<T>` is quite large. It is expected that you
/// store it behind an [`Arc`](std::sync::Arc) or similar.
pub struct Vec<T> {
raw: raw::Vec<T>,
}
impl<T> Default for Vec<T> {
fn default() -> Vec<T> {
Vec::new()
}
}
impl<T> Vec<T> {
/// Constructs a new, empty `Vec<T>`.
///
/// # Examples
///
/// ```
/// let vec: boxcar::Vec<i32> = boxcar::Vec::new();
/// ```
pub fn new() -> Vec<T> {
Vec::with_capacity(0)
}
/// Constructs a new, empty `Vec<T>` with the specified capacity.
///
/// The vector will be able to hold at least `capacity` elements
/// without reallocating.
///
/// # Examples
///
/// ```
/// let vec = boxcar::Vec::with_capacity(10);
///
/// for i in 0..10 {
/// // will not allocate
/// vec.push(i);
/// }
///
/// // may allocate
/// vec.push(11);
/// ```
pub fn with_capacity(capacity: usize) -> Vec<T> {
Vec {
raw: raw::Vec::with_capacity(capacity),
}
}
/// Reserves capacity for at least `additional` more elements to be inserted
/// in the given `Vec<T>`. The collection may reserve more space to avoid
/// frequent reallocations.
///
/// Does nothing if capacity is already sufficient.
///
/// # Examples
///
/// ```
/// let vec = boxcar::Vec::new();
/// vec.reserve(10);
///
/// for i in 0..10 {
/// // will not allocate
/// vec.push(i);
/// }
///
/// // may allocate
/// vec.push(11);
/// ```
pub fn reserve(&self, additional: usize) {
self.raw.reserve(additional)
}
/// Appends an element to the back of the vector,
/// returning the index it was inserted into.
///
/// # Examples
///
/// ```
/// let vec = boxcar::vec![1, 2];
/// assert_eq!(vec.push(3), 2);
/// assert_eq!(vec, [1, 2, 3]);
/// ```
pub fn push(&self, value: T) -> usize {
self.raw.push(value)
}
/// Returns the number of elements in the vector.
///
/// Note that due to concurrent writes, it is not guaranteed
/// that all elements `0..vec.count()` are initialized.
///
/// # Examples
///
/// ```
/// let vec = boxcar::Vec::new();
/// assert_eq!(vec.count(), 0);
/// vec.push(1);
/// vec.push(2);
/// assert_eq!(vec.count(), 2);
/// ```
#[inline]
pub fn count(&self) -> usize {
self.raw.count()
}
/// Returns `true` if the vector contains no elements.
///
/// # Examples
///
/// ```
/// let vec = boxcar::Vec::new();
/// assert!(vec.is_empty());
///
/// vec.push(1);
/// assert!(!vec.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.count() == 0
}
/// Returns a reference to the element at the given index.
///
/// # Examples
///
/// ```
/// let vec = boxcar::vec![10, 40, 30];
/// assert_eq!(Some(&40), vec.get(1));
/// assert_eq!(None, vec.get(3));
/// ```
pub fn get(&self, index: usize) -> Option<&T> {
self.raw.get(index)
}
/// Returns a mutable reference to the element at the given index.
///
/// # Examples
///
/// ```
/// let mut vec = boxcar::vec![10, 40, 30];
/// assert_eq!(Some(&mut 40), vec.get_mut(1));
/// assert_eq!(None, vec.get_mut(3));
/// ```
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
self.raw.get_mut(index)
}
/// Returns a reference to an element, without doing bounds
/// checking or verifying that the element is fully initialized.
///
/// For a safe alternative see [`get`](Vec::get).
///
/// # Safety
///
/// Calling this method with an out-of-bounds index, or for an element that
/// is being concurrently initialized is **undefined behavior**, even if
/// the resulting reference is not used.
///
/// # Examples
///
/// ```
/// let vec = boxcar::vec![1, 2, 4];
///
/// unsafe {
/// assert_eq!(vec.get_unchecked(1), &2);
/// }
/// ```
pub unsafe fn get_unchecked(&self, index: usize) -> &T {
// SAFETY: guaranteed by caller
unsafe { self.raw.get_unchecked(index) }
}
/// Returns a mutable reference to an element, without doing bounds
/// checking or verifying that the element is fully initialized.
///
/// For a safe alternative see [`get`](Vec::get).
///
/// # Safety
///
/// Calling this method with an out-of-bounds index is **undefined
/// behavior**, even if the resulting reference is not used.
///
/// # Examples
///
/// ```
/// let mut vec = boxcar::vec![1, 2, 4];
///
/// unsafe {
/// assert_eq!(vec.get_unchecked_mut(1), &mut 2);
/// }
/// ```
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
// SAFETY: guaranteed by caller
unsafe { self.raw.get_unchecked_mut(index) }
}
/// Returns an iterator over the vector.
///
/// Values are yielded in the form `(index, value)`. The vector may
/// have in-progress concurrent writes that create gaps, so `index`
/// may not be strictly sequential.
///
/// # Examples
///
/// ```
/// let vec = boxcar::vec![1, 2, 4];
/// let mut iterator = vec.iter();
///
/// assert_eq!(iterator.next(), Some((0, &1)));
/// assert_eq!(iterator.next(), Some((1, &2)));
/// assert_eq!(iterator.next(), Some((2, &4)));
/// assert_eq!(iterator.next(), None);
/// ```
pub fn iter(&self) -> Iter<'_, T> {
Iter {
vec: &self.raw,
raw: self.raw.iter(),
}
}
}
impl<T> Index<usize> for Vec<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
&self.raw[index]
}
}
impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
IntoIter {
raw: self.raw.iter(),
vec: self.raw,
}
}
}
impl<'a, T> IntoIterator for &'a Vec<T> {
type Item = (usize, &'a T);
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
/// An iterator that moves out of a vector.
///
/// This struct is created by the `into_iter` method on [`Vec`]
/// (provided by the [`IntoIterator`] trait).
pub struct IntoIter<T> {
vec: raw::Vec<T>,
raw: raw::Iter,
}
impl<T> Iterator for IntoIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
unsafe { self.raw.next_owned(&mut self.vec) }
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.raw.yielded(), Some(self.raw.yielded()))
}
}
/// An iterator over the elements of a [`Vec<T>`].
///
/// See [`Vec::iter`] for details.
pub struct Iter<'a, T> {
vec: &'a raw::Vec<T>,
raw: raw::Iter,
}
impl<'a, T> Clone for Iter<'a, T> {
fn clone(&self) -> Iter<'a, T> {
Iter {
vec: self.vec,
raw: self.raw.clone(),
}
}
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = (usize, &'a T);
fn next(&mut self) -> Option<Self::Item> {
self.raw.next_shared(self.vec)
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.vec.count() - self.raw.yielded(), None)
}
}
impl<'a, T: fmt::Debug> fmt::Debug for Iter<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct Contents<'a, T>(&'a T);
impl<T> fmt::Debug for Contents<'_, T>
where
T: Iterator + Clone,
T::Item: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.0.clone()).finish()
}
}
f.debug_tuple("Iter").field(&Contents(self)).finish()
}
}
impl<T> FromIterator<T> for Vec<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let iter = iter.into_iter();
let (lower, _) = iter.size_hint();
let vec = Vec::with_capacity(lower);
for value in iter {
vec.push(value);
}
vec
}
}
impl<T> Extend<T> for Vec<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
let iter = iter.into_iter();
let (lower, _) = iter.size_hint();
self.reserve(lower);
for value in iter {
self.push(value);
}
}
}
impl<T: Clone> Clone for Vec<T> {
fn clone(&self) -> Vec<T> {
self.iter().map(|(_, x)| x).cloned().collect()
}
}
impl<T: fmt::Debug> fmt::Debug for Vec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl<T: PartialEq> PartialEq for Vec<T> {
fn eq(&self, other: &Self) -> bool {
if self.count() != other.count() {
return false;
}
// ensure indexes are checked along with values to handle gaps in the vector
for (index, value) in self.iter() {
if other.get(index) != Some(value) {
return false;
}
}
true
}
}
impl<A, T> PartialEq<A> for Vec<T>
where
A: AsRef<[T]>,
T: PartialEq,
{
fn eq(&self, other: &A) -> bool {
let other = other.as_ref();
if self.count() != other.len() {
return false;
}
// ensure indexes are checked along with values to handle gaps in the vector
for (index, value) in self.iter() {
if other.get(index) != Some(value) {
return false;
}
}
true
}
}
impl<T: Eq> Eq for Vec<T> {}