Skip to content

Conversation

@2moe
Copy link
Contributor

@2moe 2moe commented Feb 20, 2025

  1. rustfmt.toml: replace deprecated fn_args_layout with fn_args_layout
    see also: Rename fn_args_layout config option rust-lang/rustfmt#4149

  2. benches/smallvec.rs: After running cargo +nightly fmt, the format was automatically changed.

  3. src/tinyvec.rs:

  • add impl<A: Array> Into<Vec<A::Item>> for TinyVec<A>
  • add into_vec() & into_boxed_slice()

into_vec()

The specific implementation is in the into() method of impl Into<Vec<A::Item>>.

into_boxed_slice()

Converts a TinyVec<[T; N]> into a Box<[T]>.

  • For TinyVec::Heap(Vec<T>), it takes the Vec<T> and converts it into
    a Box<[T]> without heap reallocation.
  • For TinyVec::Inline(inner_data), it first converts the inner_data to
    Vec<T>, then into a Box<[T]>. Requiring only a single heap
    allocation.

Example

use core::mem::size_of_val as mem_size_of;
use tinyvec::TinyVec;

// Initialize TinyVec with 256 elements (exceeding inline capacity)
let v: TinyVec<[_; 128]> = (0u8..=255).collect();

assert!(v.is_heap());
assert_eq!(mem_size_of(&v), 136); // mem size of TinyVec<[u8; N]>: N+8
assert_eq!(v.len(), 256);

let boxed = v.into_boxed_slice();
assert_eq!(mem_size_of(&boxed), 16); // mem size of Box<[u8]>: 16 bytes (fat pointer)
assert_eq!(boxed.len(), 256);

Closes #205

Copy link
Owner

@Lokathor Lokathor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just one little typo in the doc comments

src/tinyvec.rs Outdated
}
}

/// Converts a `TinyVec<[T; N]>` into a `Box<T>`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should say Box<[T]>

@Lokathor
Copy link
Owner

oops, might want to pick smaller array sizes for the doctest example

@2moe
Copy link
Contributor Author

2moe commented Feb 20, 2025

oops, might want to pick smaller array sizes for the doctest example

Now, TinyVec<[_; 102400]> -> TinyVec<[_; 128]>

@2moe
Copy link
Contributor Author

2moe commented Feb 20, 2025

When I changed the doc comment: Box<T> -> Box<[T]>, I forgot to run cargo fmt, which caused incorrect formatting.

I re-ran cargo fmt in smaller array sizes.

@Lokathor Lokathor merged commit 31c6f67 into Lokathor:main Mar 3, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggestion: Implement into_boxed_slice() for TinyVec

2 participants