Skip to content

Commit

Permalink
Merge pull request #24 from andylokandy/fix
Browse files Browse the repository at this point in the history
fix: add no_std while 'std' feature is opt-out
  • Loading branch information
andylokandy authored Sep 26, 2023
2 parents d3f735e + 04ba88d commit 5f33267
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
strategy:
matrix:
include:
- rust: 1.36.0 # MSRV
- rust: 1.56.0 # MSRV
features: "\"\""
- rust: 1.36.0 # MSRV
- rust: 1.56.0 # MSRV
features: "std"
- rust: stable
features: "\"\""
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "smallbox"
version = "0.8.1"
version = "0.8.2"
authors = ["andylokandy"]
description = "`Small Box` optimization: store small item on stack and fallback to heap for large item."
repository = "https://github.com/andylokandy/smallbox"
documentation = "https://andylokandy.github.io/smallbox"
catagory = "data-structures"
categories = ["data-structures"]
readme = "README.md"
keywords = ["box", "alloc", "dst", "stack", "no_std"]
license = "MIT"
edition = "2018"
edition = "2021"

[features]
default = ["std"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![crates.io](https://img.shields.io/crates/v/smallbox.svg)](https://crates.io/crates/smallbox)


`Small Box` optimization: store small item on stack and fallback to heap for large item. Requires Rust 1.36+.
`Small Box` optimization: store small item on stack and fallback to heap for large item. Requires Rust 1.56+.

## [**Documentation**](https://andylokandy.github.io/smallbox)

Expand Down
3 changes: 2 additions & 1 deletion benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ extern crate test;

use smallbox::space::*;
use smallbox::SmallBox;
use test::{black_box, Bencher};
use test::black_box;
use test::Bencher;

#[bench]
fn smallbox_small_item_small_space(b: &mut Bencher) {
Expand Down
10 changes: 10 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
edition = "2021"
version = "Two"
reorder_imports = true
imports_granularity = "Item"
group_imports = "StdExternalCrate"
where_single_line = true
trailing_comma = "Vertical"
overflow_delimited_expr = true
format_code_in_doc_comments = true
normalize_comments = true
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
//! Eliminate heap alloction for small items by `SmallBox`:
//!
//! ```rust
//! use smallbox::SmallBox;
//! use smallbox::space::S4;
//! use smallbox::SmallBox;
//!
//! let small: SmallBox<_, S4> = SmallBox::new([0; 2]);
//! let large: SmallBox<_, S4> = SmallBox::new([0; 32]);
Expand All @@ -88,8 +88,8 @@
//! extern crate smallbox;
//!
//! # fn main() {
//! use smallbox::SmallBox;
//! use smallbox::space::*;
//! use smallbox::SmallBox;
//!
//! let array: SmallBox<[usize], S2> = smallbox!([0usize, 1]);
//!
Expand All @@ -103,9 +103,9 @@
//! ```rust
//! # #[cfg(feature = "coerce")]
//! # {
//! use smallbox::SmallBox;
//! use smallbox::space::*;
//!
//! use smallbox::SmallBox;
//!
//! let array: SmallBox<[usize], S2> = SmallBox::new([0usize, 1]);
//!
//! assert_eq!(array.len(), 2);
Expand All @@ -121,8 +121,9 @@
//!
//! # fn main() {
//! use std::any::Any;
//! use smallbox::SmallBox;
//!
//! use smallbox::space::S2;
//! use smallbox::SmallBox;
//!
//! let num: SmallBox<dyn Any, S2> = smallbox!(1234u32);
//!
Expand Down Expand Up @@ -150,9 +151,9 @@
//! will be stored in the heap.
#![cfg_attr(feature = "coerce", feature(unsize, coerce_unsized))]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
extern crate core as std;

mod smallbox;
pub mod space;
Expand Down
88 changes: 44 additions & 44 deletions src/smallbox.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use std::any::Any;
use std::cmp::Ordering;
use std::fmt;
use std::hash::{self, Hash};
use std::marker::PhantomData;
use std::mem::{self, MaybeUninit};
use std::ops;
use std::ptr;

use ::alloc::alloc::{self, Layout};

use core::any::Any;
use core::cmp::Ordering;
use core::fmt;
use core::hash::Hash;
use core::hash::{self};
use core::marker::PhantomData;
#[cfg(feature = "coerce")]
use std::marker::Unsize;
use core::marker::Unsize;
use core::mem::MaybeUninit;
use core::mem::{self};
use core::ops;
#[cfg(feature = "coerce")]
use std::ops::CoerceUnsized;
use core::ops::CoerceUnsized;
use core::ptr;

use ::alloc::alloc::Layout;
use ::alloc::alloc;

#[cfg(feature = "coerce")]
impl<T: ?Sized + Unsize<U>, U: ?Sized, Space> CoerceUnsized<SmallBox<U, Space>>
Expand All @@ -36,8 +38,8 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized, Space> CoerceUnsized<SmallBox<U, Space>>
/// extern crate smallbox;
///
/// # fn main() {
/// use smallbox::SmallBox;
/// use smallbox::space::*;
/// use smallbox::SmallBox;
///
/// let small: SmallBox<[usize], S4> = smallbox!([0usize; 2]);
/// let large: SmallBox<[usize], S4> = smallbox!([1usize; 8]);
Expand Down Expand Up @@ -73,8 +75,8 @@ impl<T: ?Sized, Space> SmallBox<T, Space> {
/// # Example
///
/// ```
/// use smallbox::SmallBox;
/// use smallbox::space::*;
/// use smallbox::SmallBox;
///
/// let small: SmallBox<_, S4> = SmallBox::new([0usize; 2]);
/// let large: SmallBox<_, S4> = SmallBox::new([1usize; 8]);
Expand All @@ -86,18 +88,14 @@ impl<T: ?Sized, Space> SmallBox<T, Space> {
/// ```
#[inline(always)]
pub fn new(val: T) -> SmallBox<T, Space>
where
T: Sized,
{
where T: Sized {
smallbox!(val)
}

#[doc(hidden)]
#[inline]
pub unsafe fn new_unchecked<U>(val: U, ptr: *const T) -> SmallBox<T, Space>
where
U: Sized,
{
where U: Sized {
let result = Self::new_copy(&val, ptr);
mem::forget(val);
result
Expand All @@ -112,11 +110,12 @@ impl<T: ?Sized, Space> SmallBox<T, Space> {
/// # Example
///
/// ```
/// use smallbox::space::S2;
/// use smallbox::space::S4;
/// use smallbox::SmallBox;
/// use smallbox::space::{S2, S4};
///
/// let s: SmallBox::<_, S4> = SmallBox::new([0usize; 4]);
/// let m: SmallBox::<_, S2> = s.resize();
/// let s: SmallBox<_, S4> = SmallBox::new([0usize; 4]);
/// let m: SmallBox<_, S2> = s.resize();
/// ```
pub fn resize<ToSpace>(self) -> SmallBox<T, ToSpace> {
unsafe {
Expand Down Expand Up @@ -144,13 +143,13 @@ impl<T: ?Sized, Space> SmallBox<T, Space> {
/// # Example
///
/// ```
/// use smallbox::SmallBox;
/// use smallbox::space::S1;
/// use smallbox::SmallBox;
///
/// let stacked: SmallBox::<usize, S1> = SmallBox::new(0usize);
/// let stacked: SmallBox<usize, S1> = SmallBox::new(0usize);
/// assert!(!stacked.is_heap());
///
/// let heaped: SmallBox::<(usize, usize), S1> = SmallBox::new((0usize, 1usize));
/// let heaped: SmallBox<(usize, usize), S1> = SmallBox::new((0usize, 1usize));
/// assert!(heaped.is_heap());
/// ```
#[inline]
Expand All @@ -159,9 +158,7 @@ impl<T: ?Sized, Space> SmallBox<T, Space> {
}

unsafe fn new_copy<U>(val: &U, ptr: *const T) -> SmallBox<T, Space>
where
U: ?Sized,
{
where U: ?Sized {
let size = mem::size_of_val::<U>(val);
let align = mem::align_of_val::<U>(val);

Expand Down Expand Up @@ -247,22 +244,20 @@ impl<T: ?Sized, Space> SmallBox<T, Space> {
///
/// # Examples
/// ```
/// use smallbox::SmallBox;
/// use smallbox::space::S1;
/// use smallbox::SmallBox;
///
/// let stacked : SmallBox<_, S1> = SmallBox::new([21usize]);
/// let stacked: SmallBox<_, S1> = SmallBox::new([21usize]);
/// let val = stacked.into_inner();
/// assert_eq!(val[0], 21);
///
/// let boxed : SmallBox<_, S1> = SmallBox::new(vec![21, 56, 420]);
/// let boxed: SmallBox<_, S1> = SmallBox::new(vec![21, 56, 420]);
/// let val = boxed.into_inner();
/// assert_eq!(val[1], 56);
/// ```
#[inline]
pub fn into_inner(self) -> T
where
T: Sized,
{
where T: Sized {
let ret_val: T = unsafe { self.as_ptr().read() };

// Just drops the heap without dropping the boxed value
Expand All @@ -288,9 +283,10 @@ impl<Space> SmallBox<dyn Any, Space> {
/// extern crate smallbox;
///
/// # fn main() {
/// use std::any::Any;
/// use smallbox::SmallBox;
/// use core::any::Any;
///
/// use smallbox::space::*;
/// use smallbox::SmallBox;
///
/// fn print_if_string(value: SmallBox<dyn Any, S1>) {
/// if let Ok(string) = value.downcast::<String>() {
Expand Down Expand Up @@ -325,9 +321,10 @@ impl<Space> SmallBox<dyn Any + Send, Space> {
/// extern crate smallbox;
///
/// # fn main() {
/// use std::any::Any;
/// use smallbox::SmallBox;
/// use core::any::Any;
///
/// use smallbox::space::*;
/// use smallbox::SmallBox;
///
/// fn print_if_string(value: SmallBox<dyn Any, S1>) {
/// if let Ok(string) = value.downcast::<String>() {
Expand Down Expand Up @@ -379,8 +376,7 @@ impl<T: ?Sized, Space> ops::Drop for SmallBox<T, Space> {
}

impl<T: Clone, Space> Clone for SmallBox<T, Space>
where
T: Sized,
where T: Sized
{
fn clone(&self) -> Self {
let val: &T = &*self;
Expand Down Expand Up @@ -452,9 +448,13 @@ unsafe impl<T: ?Sized + Sync, Space> Sync for SmallBox<T, Space> {}

#[cfg(test)]
mod tests {
use core::any::Any;

use ::alloc::boxed::Box;
use ::alloc::vec;

use super::SmallBox;
use crate::space::*;
use std::any::Any;

#[test]
fn test_basic() {
Expand Down Expand Up @@ -533,7 +533,7 @@ mod tests {

#[test]
fn test_drop() {
use std::cell::Cell;
use core::cell::Cell;

struct Struct<'a>(&'a Cell<bool>, u8);
impl<'a> Drop for Struct<'a> {
Expand Down
36 changes: 36 additions & 0 deletions taplo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## https://taplo.tamasfe.dev/configuration/file.html

include = ["**/Cargo.toml"]

[formatting]
# Align consecutive entries vertically.
align_entries = false
# Append trailing commas for multi-line arrays.
array_trailing_comma = true
# Expand arrays to multiple lines that exceed the maximum column width.
array_auto_expand = true
# Collapse arrays that don't exceed the maximum column width and don't contain comments.
array_auto_collapse = false
# Omit white space padding from single-line arrays
compact_arrays = true
# Omit white space padding from the start and end of inline tables.
compact_inline_tables = false
# Maximum column width in characters, affects array expansion and collapse, this doesn't take whitespace into account.
# Note that this is not set in stone, and works on a best-effort basis.
column_width = 120
# Indent based on tables and arrays of tables and their subtables, subtables out of order are not indented.
indent_tables = false
# The substring that is used for indentation, should be tabs or spaces (but technically can be anything).
indent_string = ' '
# Add trailing newline at the end of the file if not present.
trailing_newline = true
# Alphabetically reorder keys that are not separated by empty lines.
reorder_keys = false
# Maximum amount of allowed consecutive blank lines. This does not affect the whitespace at the end of the document, as it is always stripped.
allowed_blank_lines = 1
# Use CRLF for line endings.
crlf = false

[[rule]]
keys = ["dependencies", "dev-dependencies", "build-dependencies"]
formatting = { reorder_keys = true }

0 comments on commit 5f33267

Please sign in to comment.