Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
so that it shows on doc.rs
  • Loading branch information
RustyYato committed Dec 4, 2020
1 parent fdb15b2 commit 0a25018
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
*.lock
*.lock
regex*
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'generic-vec'
version = '0.1.1'
version = '0.1.2'
authors = ['RustyYato <[email protected]>']
edition = '2018'
license = 'MIT/Apache-2.0'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ is either exactly the same as `Vec` or slightly adapted to better fit `GenericVe
Note on implementation: large parts of the implementation came straight from `Vec`
so thanks for the amazing reference `std`!

Current version: 0.1.1
Current version: 0.1.2

License: MIT/Apache-2.0
14 changes: 8 additions & 6 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ impl<T, S: ?Sized + Storage<T>> BorrowMut<[T]> for GenericVec<T, S> {
fn borrow_mut(&mut self) -> &mut [T] { self }
}

#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
impl<T, const N: usize> From<[T; N]> for crate::ArrayVec<T, N> {
fn from(array: [T; N]) -> Self { Self::from_array(array) }
}

#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
impl<T: Copy, const N: usize> From<[T; N]> for crate::InitArrayVec<T, N> {
fn from(array: [T; N]) -> Self { crate::InitArrayVec::<T, N>::new(array) }
}

#[cfg(not(doc))]
#[cfg(feature = "alloc")]
#[cfg(not(feature = "nightly"))]
impl<T> From<Vec<T>> for crate::HeapVec<T> {
Expand All @@ -107,8 +108,8 @@ impl<T> From<Vec<T>> for crate::HeapVec<T> {
}
}

#[cfg(feature = "alloc")]
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "alloc"))]
#[cfg(any(doc, feature = "nightly"))]
impl<T, A: std::alloc::AllocRef> From<Vec<T, A>> for crate::HeapVec<T, A> {
fn from(vec: Vec<T, A>) -> Self {
let (ptr, len, cap, alloc) = vec.into_raw_parts_with_alloc();
Expand All @@ -122,6 +123,7 @@ impl<T, A: std::alloc::AllocRef> From<Vec<T, A>> for crate::HeapVec<T, A> {
}
}

#[cfg(not(doc))]
#[cfg(feature = "alloc")]
#[cfg(not(feature = "nightly"))]
impl<T> From<crate::HeapVec<T>> for Vec<T> {
Expand All @@ -133,8 +135,8 @@ impl<T> From<crate::HeapVec<T>> for Vec<T> {
}
}

#[cfg(feature = "alloc")]
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "alloc"))]
#[cfg(any(doc, feature = "nightly"))]
impl<T, A: std::alloc::AllocRef> From<crate::HeapVec<T, A>> for Vec<T, A> {
fn from(vec: crate::HeapVec<T, A>) -> Self {
let (length, alloc) = vec.into_raw_parts();
Expand Down
2 changes: 1 addition & 1 deletion src/iter/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ impl<'a, T, S: ?Sized + Storage<T>> Drain<'a, T, S> {

impl<T, S: ?Sized + Storage<T>> FusedIterator for Drain<'_, T, S> {}

#[cfg(feature = "nightly")]
impl<T, S: ?Sized + Storage<T>> ExactSizeIterator for Drain<'_, T, S> {
#[cfg(feature = "nightly")]
fn is_empty(&self) -> bool { self.raw.is_empty() }
}

Expand Down
42 changes: 21 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,24 @@ use raw::Storage;
pub use core;

/// A heap backed vector with a growable capacity
#[cfg(all(feature = "alloc", feature = "nightly"))]
#[cfg(any(doc, all(feature = "alloc", feature = "nightly")))]
#[cfg_attr(doc, doc(cfg(all(feature = "alloc", feature = "nightly"))))]
pub type HeapVec<T, A = std::alloc::Global> = GenericVec<T, raw::Heap<T, A>>;

/// A heap backed vector with a growable capacity
#[cfg(all(feature = "alloc", not(feature = "nightly")))]
#[cfg(all(not(doc), feature = "alloc", not(feature = "nightly")))]
#[cfg_attr(doc, doc(cfg(feature = "alloc")))]
pub type HeapVec<T> = GenericVec<T, raw::Heap<T>>;

/// An array backed vector backed by potentially uninitialized memory
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
#[cfg_attr(doc, doc(cfg(feature = "nightly")))]
pub type ArrayVec<T, const N: usize> = TypeVec<T, [T; N]>;
/// An slice backed vector backed by potentially uninitialized memory
pub type SliceVec<'a, T> = GenericVec<T, &'a mut raw::UninitSlice<T>>;

/// An array backed vector backed by initialized memory
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
#[cfg_attr(doc, doc(cfg(feature = "nightly")))]
pub type InitArrayVec<T, const N: usize> = GenericVec<T, [T; N]>;
/// An slice backed vector backed by initialized memory
Expand Down Expand Up @@ -492,7 +492,7 @@ impl<T, B, A> TypeVec<T, B, A> {
}
}

#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
#[cfg_attr(doc, doc(cfg(feature = "nightly")))]
impl<T, const N: usize> ArrayVec<T, N> {
/// Create a new full `ArrayVec`
Expand Down Expand Up @@ -542,28 +542,28 @@ impl<T> HeapVec<T> {
}
}

#[cfg(all(feature = "nightly", feature = "alloc"))]
#[cfg(any(doc, all(feature = "nightly", feature = "alloc")))]
#[cfg_attr(doc, doc(cfg(all(feature = "nightly", feature = "alloc"))))]
impl<T, A: std::alloc::AllocRef> HeapVec<T, A> {
/// Create a new empty `HeapVec` with the given allocator
pub fn with_alloc(alloc: A) -> Self { Self::with_storage(raw::Heap::with_alloc(alloc)) }
}

#[cfg(not(feature = "nightly"))]
#[cfg(any(doc, not(feature = "nightly")))]
impl<'a, T> SliceVec<'a, T> {
/// Create a new empty `SliceVec`
pub fn new(slice: &'a mut [MaybeUninit<T>]) -> Self { Self::with_storage(raw::UninitSlice::from_mut(slice)) }
}

#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
impl<'a, T> SliceVec<'a, T> {
/// Create a new empty `SliceVec`
///
/// Note: this is only const with the `nightly` feature enabled
pub const fn new(slice: &'a mut [MaybeUninit<T>]) -> Self { Self::with_storage(raw::UninitSlice::from_mut(slice)) }
}

#[cfg(not(feature = "nightly"))]
#[cfg(any(doc, not(feature = "nightly")))]
impl<'a, T: Copy> InitSliceVec<'a, T> {
/// Create a new full `InitSliceVec`
pub fn new(storage: &'a mut [T]) -> Self {
Expand Down Expand Up @@ -986,7 +986,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// # Panic
///
/// May panic or reallocate if the collection has less than N elements remaining
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub fn push_array<const N: usize>(&mut self, value: [T; N]) -> &mut [T; N] {
self.reserve(N);

Expand Down Expand Up @@ -1032,7 +1032,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
///
/// * May panic or reallocate if the collection has less than N elements remaining
/// * Panics if index > len.
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub fn insert_array<const N: usize>(&mut self, index: usize, value: [T; N]) -> &mut [T; N] {
#[cold]
#[inline(never)]
Expand Down Expand Up @@ -1083,7 +1083,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// # Panics
///
/// Panics if the collection contains less than `N` elements in it
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub fn pop_array<const N: usize>(&mut self) -> [T; N] {
#[cold]
#[inline(never)]
Expand Down Expand Up @@ -1130,7 +1130,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// # Panics
///
/// Panics if `index` is out of bounds or if `index + N > len()`
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub fn remove_array<const N: usize>(&mut self, index: usize) -> [T; N] {
#[cold]
#[inline(never)]
Expand Down Expand Up @@ -1199,7 +1199,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// to hold `N` elements.
///
/// Guaranteed to not panic/abort/allocate
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub fn try_push_array<const N: usize>(&mut self, value: [T; N]) -> Result<&mut [T; N], [T; N]> {
if self.remaining_capacity() < N {
Err(value)
Expand Down Expand Up @@ -1234,7 +1234,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// to hold `N` elements or index is out of bounds
///
/// Guaranteed to not panic/abort/allocate
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub fn try_insert_array<const N: usize>(&mut self, index: usize, value: [T; N]) -> Result<&mut [T; N], [T; N]> {
if self.capacity().wrapping_sub(self.len()) < N || index > self.len() {
Err(value)
Expand Down Expand Up @@ -1266,7 +1266,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// Returns `None` if the collection is has less than N elements
///
/// Guaranteed to not panic/abort/allocate
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub fn try_pop_array<const N: usize>(&mut self) -> Option<[T; N]> {
if self.is_empty() {
None
Expand Down Expand Up @@ -1300,7 +1300,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// or `index` is out of bounds.
///
/// Guaranteed to not panic/abort/allocate
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub fn try_remove_array<const N: usize>(&mut self, index: usize) -> Option<[T; N]> {
if self.len() < index || self.len().wrapping_sub(index) < N {
None
Expand Down Expand Up @@ -1366,7 +1366,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// # Safety
///
/// the collection's remaining capacity must be at least N
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub unsafe fn push_array_unchecked<const N: usize>(&mut self, value: [T; N]) -> &mut [T; N] {
match S::CONST_CAPACITY {
Some(n) if n < N => {
Expand Down Expand Up @@ -1422,7 +1422,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
///
/// * the collection's remaining capacity must be at least N
/// * hte index must be in bounds
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub unsafe fn insert_array_unchecked<const N: usize>(&mut self, index: usize, value: [T; N]) -> &mut [T; N] {
match S::CONST_CAPACITY {
Some(n) if n < N => {
Expand Down Expand Up @@ -1481,7 +1481,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
/// # Safety
///
/// The collection must contain at least `N` elements in it
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub unsafe fn pop_array_unchecked<const N: usize>(&mut self) -> [T; N] {
match S::CONST_CAPACITY {
Some(n) if n < N => panic!("Tried to remove {} elements from a {} capacity vector!", N, n),
Expand Down Expand Up @@ -1546,7 +1546,7 @@ impl<T, S: ?Sized + Storage<T>> GenericVec<T, S> {
///
/// the collection must contain at least N elements, and
/// index must be in bounds
#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub unsafe fn remove_array_unchecked<const N: usize>(&mut self, index: usize) -> [T; N] {
match S::CONST_CAPACITY {
Some(n) if n < N => panic!("Tried to remove {} elements from a {} capacity vector!", N, n),
Expand Down
10 changes: 5 additions & 5 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
use std::boxed::Box;

mod array;
#[cfg(feature = "alloc")]
#[cfg(any(doc, feature = "alloc"))]
mod heap;
mod slice;
mod uninit;
mod zero_sized;

mod capacity;

#[cfg(feature = "alloc")]
#[cfg(any(doc, feature = "alloc"))]
pub use heap::Heap;

pub use slice::UninitSlice;
Expand Down Expand Up @@ -106,9 +106,9 @@ unsafe impl<T, S: ?Sized + Storage<T>> Storage<T> for &mut S {
fn try_reserve(&mut self, new_capacity: usize) -> bool { S::try_reserve(self, new_capacity) }
}

#[cfg(feature = "alloc")]
#[cfg(any(doc, feature = "alloc"))]
unsafe impl<T, S: ?Sized + StorageInit<T>> StorageInit<T> for Box<S> {}
#[cfg(feature = "alloc")]
#[cfg(any(doc, feature = "alloc"))]
unsafe impl<T, S: ?Sized + Storage<T>> Storage<T> for Box<S> {
#[doc(hidden)]
const CONST_CAPACITY: Option<usize> = S::CONST_CAPACITY;
Expand All @@ -126,7 +126,7 @@ unsafe impl<T, S: ?Sized + Storage<T>> Storage<T> for Box<S> {
fn try_reserve(&mut self, new_capacity: usize) -> bool { S::try_reserve(self, new_capacity) }
}

#[cfg(feature = "alloc")]
#[cfg(any(doc, feature = "alloc"))]
unsafe impl<T, S: ?Sized + StorageWithCapacity<T>> StorageWithCapacity<T> for Box<S> {
#[inline(always)]
fn with_capacity(capacity: usize) -> Self { Box::new(S::with_capacity(capacity)) }
Expand Down
8 changes: 4 additions & 4 deletions src/raw/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ macro_rules! doc_heap {
}
}

#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
mod nightly;
#[cfg(not(feature = "nightly"))]
#[cfg(not(any(doc, feature = "nightly")))]
mod stable;

#[cfg(feature = "nightly")]
#[cfg(any(doc, feature = "nightly"))]
pub use nightly::Heap;
#[cfg(not(feature = "nightly"))]
#[cfg(not(any(doc, feature = "nightly")))]
pub use stable::Heap;

const INIT_ALLOC_CAPACITY: usize = 4;
1 change: 0 additions & 1 deletion src/raw/heap/nightly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use core::{
};
use std::alloc::handle_alloc_error;

#[cfg(feature = "nightly")]
use std::alloc::{AllocRef, Global};

doc_heap! {
Expand Down

0 comments on commit 0a25018

Please sign in to comment.