Skip to content

make automatic length detection feature-gated #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: generic-length-type-over-new-layout
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ harness = false
[features]
default = ["std"]
std = []
nightly = []

[profile.bench]
debug = true
Expand Down
20 changes: 16 additions & 4 deletions src/len_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ macro_rules! impl_lenuint {
impl $LenUint for $ty {
const MAX: usize = <$ty>::MAX as usize;
const ZERO: Self = 0;
fn from_usize(n: usize) -> Self { n as $ty }
fn to_usize(self) -> usize { self as usize }
fn from_usize(n: usize) -> Self {
n as $ty
}
fn to_usize(self) -> usize {
self as usize
}
}
};
}

macro_rules! impl_default_lentype_from_cap {
($LenT:ty => $($CAP:literal),*) => {
$(
#[cfg(feature = "nightly")]
impl CapToDefaultLenType for ConstGenericSmuggler<$CAP> {
type T = $LenT;
}
)*
};
}

pub trait LenUint: Add + Sub + Copy + PartialOrd + PartialEq + private::Sealed {
pub trait LenUint: Add + Sub + Copy + PartialOrd + PartialEq + private::Sealed {
const MAX: usize;
const ZERO: Self;
fn from_usize(n: usize) -> Self;
Expand All @@ -46,9 +51,16 @@ pub trait CapToDefaultLenType {
type T: LenUint;
}

impl<const CAP: usize> CapToDefaultLenType for ConstGenericSmuggler<CAP> {
#[cfg(not(feature = "nightly"))]
type T = u32;
#[cfg(feature = "nightly")]
default type T = u32;
}

impl_default_lentype_from_cap!(u8 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64, 100, 128, 200, 255);
impl_default_lentype_from_cap!(u16 => 256, 500, 512, 1000, 1024, 2048, 4096, 8192, 16384, 32768, 65535);
impl_default_lentype_from_cap!(u32 => 65536, 1000000, 4294967295);
// impl_default_lentype_from_cap!(u32 => 65536, 1000000, 4294967295);
impl_default_lentype_from_cap!(u64 => 18446744073709551615);

pub trait CapFitsInLenType {
Expand Down
19 changes: 10 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! **arrayvec** provides the types [`ArrayVec`] and [`ArrayString`]:
//! **arrayvec** provides the types [`ArrayVec`] and [`ArrayString`]:
//! array-backed vector and string types, which store their contents inline.
//!
//! The arrayvec package has the following cargo features:
Expand All @@ -19,25 +19,26 @@
//!
//! This version of arrayvec requires Rust 1.51 or later.
//!
#![doc(html_root_url="https://docs.rs/arrayvec/0.7/")]
#![cfg_attr(not(feature="std"), no_std)]
#![doc(html_root_url = "https://docs.rs/arrayvec/0.7/")]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "nightly", feature(specialization))]

#[cfg(feature="serde")]
#[cfg(feature = "serde")]
extern crate serde;

#[cfg(not(feature="std"))]
#[cfg(not(feature = "std"))]
extern crate core as std;

mod len_type;
mod arrayvec_impl;
mod arrayvec;
mod array_string;
mod arrayvec;
mod arrayvec_impl;
mod char;
mod errors;
mod len_type;
mod utils;

pub use crate::array_string::ArrayString;
pub use crate::errors::CapacityError;
pub use len_type::LenUint;

pub use crate::arrayvec::{ArrayVec, IntoIter, Drain};
pub use crate::arrayvec::{ArrayVec, Drain, IntoIter};