diff --git a/Cargo.lock b/Cargo.lock index 5fb6cff0e26e..2c421451213d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -651,6 +651,7 @@ dependencies = [ "test-fixture", "test-utils", "text-size", + "thin-vec", "tracing", "triomphe", "tt", @@ -2330,6 +2331,12 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f18aa187839b2bdb1ad2fa35ead8c4c2976b64e4363c386d45ac0f7ee85c9233" +[[package]] +name = "thin-vec" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "144f754d318415ac792f9d69fc87abbbfc043ce2ef041c60f16ad828f638717d" + [[package]] name = "thiserror" version = "1.0.69" diff --git a/crates/hir-def/Cargo.toml b/crates/hir-def/Cargo.toml index 98d24d20b0cd..f97597ffe5a6 100644 --- a/crates/hir-def/Cargo.toml +++ b/crates/hir-def/Cargo.toml @@ -43,6 +43,7 @@ mbe.workspace = true cfg.workspace = true tt.workspace = true span.workspace = true +thin-vec = "0.2.14" [dev-dependencies] expect-test.workspace = true diff --git a/crates/hir-def/src/generics.rs b/crates/hir-def/src/generics.rs index ee026509a2cf..58ac86e8a605 100644 --- a/crates/hir-def/src/generics.rs +++ b/crates/hir-def/src/generics.rs @@ -12,11 +12,9 @@ use hir_expand::{ }; use intern::sym; use la_arena::{Arena, RawIdx}; -use stdx::{ - impl_from, - thin_vec::{EmptyOptimizedThinVec, ThinVec}, -}; +use stdx::impl_from; use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds}; +use thin_vec::ThinVec; use triomphe::Arc; use crate::{ @@ -753,12 +751,17 @@ fn copy_type_ref( ) -> TypeRefId { let result = match &from[type_ref] { TypeRef::Fn(fn_) => { - let params = fn_.params().iter().map(|(name, param_type)| { + let params = fn_.params.iter().map(|(name, param_type)| { (name.clone(), copy_type_ref(*param_type, from, from_source_map, to, to_source_map)) }); - TypeRef::Fn(FnType::new(fn_.is_varargs(), fn_.is_unsafe(), fn_.abi().clone(), params)) + TypeRef::Fn(Box::new(FnType { + params: params.collect(), + is_varargs: fn_.is_varargs, + is_unsafe: fn_.is_unsafe, + abi: fn_.abi.clone(), + })) } - TypeRef::Tuple(types) => TypeRef::Tuple(EmptyOptimizedThinVec::from_iter( + TypeRef::Tuple(types) => TypeRef::Tuple(ThinVec::from_iter( types.iter().map(|&t| copy_type_ref(t, from, from_source_map, to, to_source_map)), )), &TypeRef::RawPtr(type_ref, mutbl) => TypeRef::RawPtr( @@ -817,13 +820,17 @@ fn copy_path( Path::BarePath(mod_path) => Path::BarePath(mod_path.clone()), Path::Normal(path) => { let type_anchor = path - .type_anchor() + .type_anchor .map(|type_ref| copy_type_ref(type_ref, from, from_source_map, to, to_source_map)); - let mod_path = path.mod_path().clone(); - let generic_args = path.generic_args().iter().map(|generic_args| { + let mod_path = path.mod_path.clone(); + let generic_args = path.generic_args.iter().map(|generic_args| { copy_generic_args(generic_args, from, from_source_map, to, to_source_map) }); - Path::Normal(NormalPath::new(type_anchor, mod_path, generic_args)) + Path::Normal(Box::new(NormalPath { + generic_args: generic_args.collect(), + type_anchor, + mod_path, + })) } Path::LangItem(lang_item, name) => Path::LangItem(*lang_item, name.clone()), } @@ -879,7 +886,7 @@ fn copy_type_bounds<'a>( from_source_map: &'a TypesSourceMap, to: &'a mut TypesMap, to_source_map: &'a mut TypesSourceMap, -) -> impl stdx::thin_vec::TrustedLen + 'a { +) -> impl Iterator + 'a { bounds.iter().map(|bound| copy_type_bound(bound, from, from_source_map, to, to_source_map)) } diff --git a/crates/hir-def/src/hir/type_ref.rs b/crates/hir-def/src/hir/type_ref.rs index 7bb558d34563..fd50d2f00989 100644 --- a/crates/hir-def/src/hir/type_ref.rs +++ b/crates/hir-def/src/hir/type_ref.rs @@ -12,11 +12,11 @@ use hir_expand::{ use intern::{Symbol, sym}; use la_arena::{Arena, ArenaMap, Idx}; use span::Edition; -use stdx::thin_vec::{EmptyOptimizedThinVec, ThinVec, thin_vec_with_header_struct}; use syntax::{ AstPtr, ast::{self, HasGenericArgs, HasName, IsString}, }; +use thin_vec::ThinVec; use crate::{ SyntheticSyntax, @@ -120,13 +120,12 @@ impl TraitRef { } } -thin_vec_with_header_struct! { - pub new(pub(crate)) struct FnType, FnTypeHeader { - pub params: [(Option, TypeRefId)], - pub is_varargs: bool, - pub is_unsafe: bool, - pub abi: Option; ref, - } +#[derive(Clone, PartialEq, Eq, Hash, Debug)] +pub struct FnType { + pub params: Box<[(Option, TypeRefId)]>, + pub is_varargs: bool, + pub is_unsafe: bool, + pub abi: Option, } #[derive(Clone, PartialEq, Eq, Hash, Debug)] @@ -148,14 +147,14 @@ pub struct RefType { pub enum TypeRef { Never, Placeholder, - Tuple(EmptyOptimizedThinVec), + Tuple(ThinVec), Path(Path), RawPtr(TypeRefId, Mutability), Reference(Box), Array(Box), Slice(TypeRefId), /// A fn pointer. Last element of the vector is the return type. - Fn(FnType), + Fn(Box), ImplTrait(ThinVec), DynTrait(ThinVec), Macro(AstId), @@ -273,9 +272,9 @@ impl TypeRef { pub fn from_ast(ctx: &mut LowerCtx<'_>, node: ast::Type) -> TypeRefId { let ty = match &node { ast::Type::ParenType(inner) => return TypeRef::from_ast_opt(ctx, inner.ty()), - ast::Type::TupleType(inner) => TypeRef::Tuple(EmptyOptimizedThinVec::from_iter( - Vec::from_iter(inner.fields().map(|it| TypeRef::from_ast(ctx, it))), - )), + ast::Type::TupleType(inner) => TypeRef::Tuple(ThinVec::from_iter(Vec::from_iter( + inner.fields().map(|it| TypeRef::from_ast(ctx, it)), + ))), ast::Type::NeverType(..) => TypeRef::Never, ast::Type::PathType(inner) => { // FIXME: Use `Path::from_src` @@ -342,7 +341,12 @@ impl TypeRef { let abi = inner.abi().map(lower_abi); params.push((None, ret_ty)); - TypeRef::Fn(FnType::new(is_varargs, inner.unsafe_token().is_some(), abi, params)) + TypeRef::Fn(Box::new(FnType { + params: params.into(), + is_varargs, + is_unsafe: inner.unsafe_token().is_some(), + abi, + })) } // for types are close enough for our purposes to the inner type for now... ast::Type::ForType(inner) => return TypeRef::from_ast_opt(ctx, inner.ty()), @@ -375,7 +379,7 @@ impl TypeRef { } pub(crate) fn unit() -> TypeRef { - TypeRef::Tuple(EmptyOptimizedThinVec::empty()) + TypeRef::Tuple(ThinVec::new()) } pub fn walk(this: TypeRefId, map: &TypesMap, f: &mut impl FnMut(&TypeRef)) { @@ -386,7 +390,7 @@ impl TypeRef { f(type_ref); match type_ref { TypeRef::Fn(fn_) => { - fn_.params().iter().for_each(|&(_, param_type)| go(param_type, f, map)) + fn_.params.iter().for_each(|&(_, param_type)| go(param_type, f, map)) } TypeRef::Tuple(types) => types.iter().for_each(|&t| go(t, f, map)), TypeRef::RawPtr(type_ref, _) | TypeRef::Slice(type_ref) => go(*type_ref, f, map), diff --git a/crates/hir-def/src/item_tree/lower.rs b/crates/hir-def/src/item_tree/lower.rs index 776ee98f3bce..686682142052 100644 --- a/crates/hir-def/src/item_tree/lower.rs +++ b/crates/hir-def/src/item_tree/lower.rs @@ -12,11 +12,11 @@ use intern::{Symbol, sym}; use la_arena::Arena; use rustc_hash::FxHashMap; use span::{AstIdMap, SyntaxContext}; -use stdx::thin_vec::ThinVec; use syntax::{ AstNode, ast::{self, HasModuleItem, HasName, HasTypeBounds, IsString}, }; +use thin_vec::ThinVec; use triomphe::Arc; use crate::{ diff --git a/crates/hir-def/src/lower.rs b/crates/hir-def/src/lower.rs index c0f6e1a6867c..b3acfe4239b8 100644 --- a/crates/hir-def/src/lower.rs +++ b/crates/hir-def/src/lower.rs @@ -3,8 +3,8 @@ use std::{cell::OnceCell, mem}; use hir_expand::{AstId, HirFileId, InFile, span_map::SpanMap}; use span::{AstIdMap, AstIdNode, Edition, EditionedFileId, FileId, RealSpanMap}; -use stdx::thin_vec::ThinVec; use syntax::ast; +use thin_vec::ThinVec; use triomphe::Arc; use crate::{ diff --git a/crates/hir-def/src/path.rs b/crates/hir-def/src/path.rs index 1f7365f9a738..7ef31d02450a 100644 --- a/crates/hir-def/src/path.rs +++ b/crates/hir-def/src/path.rs @@ -16,7 +16,6 @@ use crate::{ use hir_expand::name::Name; use intern::Interned; use span::Edition; -use stdx::thin_vec::thin_vec_with_header_struct; use syntax::ast; pub use hir_expand::mod_path::{ModPath, PathKind, path}; @@ -58,7 +57,7 @@ pub enum Path { /// this is not a problem since many more paths have generics than a type anchor). BarePath(Interned), /// `Path::Normal` will always have either generics or type anchor. - Normal(NormalPath), + Normal(Box), /// A link to a lang item. It is used in desugaring of things like `it?`. We can show these /// links via a normal path since they might be private and not accessible in the usage place. LangItem(LangItemTarget, Option), @@ -71,12 +70,11 @@ const _: () = { assert!(size_of::>() == 16); }; -thin_vec_with_header_struct! { - pub new(pub(crate)) struct NormalPath, NormalPathHeader { - pub generic_args: [Option], - pub type_anchor: Option, - pub mod_path: Interned; ref, - } +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct NormalPath { + pub generic_args: Box<[Option]>, + pub type_anchor: Option, + pub mod_path: Interned, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -143,7 +141,11 @@ impl Path { /// Converts a known mod path to `Path`. pub fn from_known_path(path: ModPath, generic_args: Vec>) -> Path { - Path::Normal(NormalPath::new(None, Interned::new(path), generic_args)) + Path::Normal(Box::new(NormalPath { + generic_args: generic_args.into_boxed_slice(), + type_anchor: None, + mod_path: Interned::new(path), + })) } /// Converts a known mod path to `Path`. @@ -155,7 +157,7 @@ impl Path { pub fn kind(&self) -> &PathKind { match self { Path::BarePath(mod_path) => &mod_path.kind, - Path::Normal(path) => &path.mod_path().kind, + Path::Normal(path) => &path.mod_path.kind, Path::LangItem(..) => &PathKind::Abs, } } @@ -163,7 +165,7 @@ impl Path { #[inline] pub fn type_anchor(&self) -> Option { match self { - Path::Normal(path) => path.type_anchor(), + Path::Normal(path) => path.type_anchor, Path::LangItem(..) | Path::BarePath(_) => None, } } @@ -171,7 +173,7 @@ impl Path { #[inline] pub fn generic_args(&self) -> Option<&[Option]> { match self { - Path::Normal(path) => Some(path.generic_args()), + Path::Normal(path) => Some(&path.generic_args), Path::LangItem(..) | Path::BarePath(_) => None, } } @@ -182,8 +184,8 @@ impl Path { PathSegments { segments: mod_path.segments(), generic_args: None } } Path::Normal(path) => PathSegments { - segments: path.mod_path().segments(), - generic_args: Some(path.generic_args()), + segments: path.mod_path.segments(), + generic_args: Some(&path.generic_args), }, Path::LangItem(_, seg) => PathSegments { segments: seg.as_slice(), generic_args: None }, } @@ -192,7 +194,7 @@ impl Path { pub fn mod_path(&self) -> Option<&ModPath> { match self { Path::BarePath(mod_path) => Some(mod_path), - Path::Normal(path) => Some(path.mod_path()), + Path::Normal(path) => Some(&path.mod_path), Path::LangItem(..) => None, } } @@ -209,12 +211,12 @@ impl Path { )))) } Path::Normal(path) => { - let mod_path = path.mod_path(); + let mod_path = &path.mod_path; if mod_path.is_ident() { return None; } - let type_anchor = path.type_anchor(); - let generic_args = path.generic_args(); + let type_anchor = path.type_anchor; + let generic_args = &path.generic_args; let qualifier_mod_path = Interned::new(ModPath::from_segments( mod_path.kind, mod_path.segments()[..mod_path.segments().len() - 1].iter().cloned(), @@ -223,11 +225,11 @@ impl Path { if type_anchor.is_none() && qualifier_generic_args.iter().all(|it| it.is_none()) { Some(Path::BarePath(qualifier_mod_path)) } else { - Some(Path::Normal(NormalPath::new( + Some(Path::Normal(Box::new(NormalPath { type_anchor, - qualifier_mod_path, - qualifier_generic_args.iter().cloned(), - ))) + mod_path: qualifier_mod_path, + generic_args: qualifier_generic_args.iter().cloned().collect(), + }))) } } Path::LangItem(..) => None, @@ -238,9 +240,9 @@ impl Path { match self { Path::BarePath(mod_path) => mod_path.is_Self(), Path::Normal(path) => { - path.type_anchor().is_none() - && path.mod_path().is_Self() - && path.generic_args().iter().all(|args| args.is_none()) + path.type_anchor.is_none() + && path.mod_path.is_Self() + && path.generic_args.iter().all(|args| args.is_none()) } Path::LangItem(..) => false, } diff --git a/crates/hir-def/src/path/lower.rs b/crates/hir-def/src/path/lower.rs index c8269db58173..78f3ec07aa3c 100644 --- a/crates/hir-def/src/path/lower.rs +++ b/crates/hir-def/src/path/lower.rs @@ -9,8 +9,8 @@ use hir_expand::{ name::{AsName, Name}, }; use intern::{Interned, sym}; -use stdx::thin_vec::EmptyOptimizedThinVec; use syntax::ast::{self, AstNode, HasGenericArgs, HasTypeBounds}; +use thin_vec::ThinVec; use crate::{ path::{ @@ -213,7 +213,11 @@ pub(super) fn lower_path(ctx: &mut LowerCtx<'_>, mut path: ast::Path) -> Option< if type_anchor.is_none() && generic_args.is_empty() { return Some(Path::BarePath(mod_path)); } else { - return Some(Path::Normal(NormalPath::new(type_anchor, mod_path, generic_args))); + return Some(Path::Normal(Box::new(NormalPath { + generic_args: generic_args.into_boxed_slice(), + type_anchor, + mod_path, + }))); } fn qualifier(path: &ast::Path) -> Option { @@ -344,7 +348,7 @@ fn lower_generic_args_from_fn_path( param_types.push(type_ref); } let args = Box::new([GenericArg::Type( - ctx.alloc_type_ref_desugared(TypeRef::Tuple(EmptyOptimizedThinVec::from_iter(param_types))), + ctx.alloc_type_ref_desugared(TypeRef::Tuple(ThinVec::from_iter(param_types))), )]); let bindings = if let Some(ret_type) = ret_type { let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty()); diff --git a/crates/hir-def/src/pretty.rs b/crates/hir-def/src/pretty.rs index c431b45dc79c..8d5f6b8b453b 100644 --- a/crates/hir-def/src/pretty.rs +++ b/crates/hir-def/src/pretty.rs @@ -220,11 +220,11 @@ pub(crate) fn print_type_ref( } TypeRef::Fn(fn_) => { let ((_, return_type), args) = - fn_.params().split_last().expect("TypeRef::Fn is missing return type"); - if fn_.is_unsafe() { + fn_.params.split_last().expect("TypeRef::Fn is missing return type"); + if fn_.is_unsafe { write!(buf, "unsafe ")?; } - if let Some(abi) = fn_.abi() { + if let Some(abi) = &fn_.abi { buf.write_str("extern ")?; buf.write_str(abi.as_str())?; buf.write_char(' ')?; @@ -236,7 +236,7 @@ pub(crate) fn print_type_ref( } print_type_ref(db, *typeref, map, buf, edition)?; } - if fn_.is_varargs() { + if fn_.is_varargs { if !args.is_empty() { write!(buf, ", ")?; } diff --git a/crates/hir-def/src/resolver.rs b/crates/hir-def/src/resolver.rs index 28ebaadf4d79..4f1be7285c75 100644 --- a/crates/hir-def/src/resolver.rs +++ b/crates/hir-def/src/resolver.rs @@ -181,7 +181,7 @@ impl Resolver { { let path = match path { Path::BarePath(mod_path) => mod_path, - Path::Normal(it) => it.mod_path(), + Path::Normal(it) => &it.mod_path, Path::LangItem(l, seg) => { let type_ns = match *l { LangItemTarget::Union(it) => TypeNs::AdtId(it.into()), @@ -304,7 +304,7 @@ impl Resolver { ) -> Option<(ResolveValueResult, ResolvePathResultPrefixInfo)> { let path = match path { Path::BarePath(mod_path) => mod_path, - Path::Normal(it) => it.mod_path(), + Path::Normal(it) => &it.mod_path, Path::LangItem(l, None) => { return Some(( ResolveValueResult::ValueNs( diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index d72b1955246e..52ed0525a2cf 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -2128,16 +2128,16 @@ impl HirDisplayWithTypesMap for TypeRefId { write!(f, "]")?; } TypeRef::Fn(fn_) => { - if fn_.is_unsafe() { + if fn_.is_unsafe { write!(f, "unsafe ")?; } - if let Some(abi) = fn_.abi() { + if let Some(abi) = &fn_.abi { f.write_str("extern \"")?; f.write_str(abi.as_str())?; f.write_str("\" ")?; } write!(f, "fn(")?; - if let Some(((_, return_type), function_parameters)) = fn_.params().split_last() { + if let Some(((_, return_type), function_parameters)) = fn_.params.split_last() { for index in 0..function_parameters.len() { let (param_name, param_type) = &function_parameters[index]; if let Some(name) = param_name { @@ -2150,8 +2150,8 @@ impl HirDisplayWithTypesMap for TypeRefId { write!(f, ", ")?; } } - if fn_.is_varargs() { - write!(f, "{}...", if fn_.params().len() == 1 { "" } else { ", " })?; + if fn_.is_varargs { + write!(f, "{}...", if fn_.params.len() == 1 { "" } else { ", " })?; } write!(f, ")")?; match &types_map[*return_type] { diff --git a/crates/hir-ty/src/infer/expr.rs b/crates/hir-ty/src/infer/expr.rs index c5a6c21d29b2..238cbe2b05de 100644 --- a/crates/hir-ty/src/infer/expr.rs +++ b/crates/hir-ty/src/infer/expr.rs @@ -198,7 +198,7 @@ impl InferenceContext<'_> { match &self.body[expr] { // Lang item paths cannot currently be local variables or statics. Expr::Path(Path::LangItem(_, _)) => false, - Expr::Path(Path::Normal(path)) => path.type_anchor().is_none(), + Expr::Path(Path::Normal(path)) => path.type_anchor.is_none(), Expr::Path(path) => self .resolver .resolve_path_in_value_ns_fully( diff --git a/crates/hir-ty/src/lower.rs b/crates/hir-ty/src/lower.rs index e5f3c4cfc8fc..9df8d9cebbe8 100644 --- a/crates/hir-ty/src/lower.rs +++ b/crates/hir-ty/src/lower.rs @@ -318,15 +318,15 @@ impl<'a> TyLoweringContext<'a> { let substs = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { Substitution::from_iter( Interner, - fn_.params().iter().map(|&(_, tr)| ctx.lower_ty(tr)), + fn_.params.iter().map(|&(_, tr)| ctx.lower_ty(tr)), ) }); TyKind::Function(FnPointer { num_binders: 0, // FIXME lower `for<'a> fn()` correctly sig: FnSig { - abi: fn_.abi().as_ref().map_or(FnAbi::Rust, FnAbi::from_symbol), - safety: if fn_.is_unsafe() { Safety::Unsafe } else { Safety::Safe }, - variadic: fn_.is_varargs(), + abi: fn_.abi.as_ref().map_or(FnAbi::Rust, FnAbi::from_symbol), + safety: if fn_.is_unsafe { Safety::Unsafe } else { Safety::Safe }, + variadic: fn_.is_varargs, }, substitution: FnSubst(substs), }) diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index ce56a819942f..982be40dd425 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -12,7 +12,6 @@ pub mod non_empty_vec; pub mod panic_context; pub mod process; pub mod rand; -pub mod thin_vec; pub mod thread; pub use itertools; diff --git a/crates/stdx/src/thin_vec.rs b/crates/stdx/src/thin_vec.rs deleted file mode 100644 index 69d8ee7d9068..000000000000 --- a/crates/stdx/src/thin_vec.rs +++ /dev/null @@ -1,468 +0,0 @@ -use std::alloc::{Layout, dealloc, handle_alloc_error}; -use std::fmt; -use std::hash::{Hash, Hasher}; -use std::marker::PhantomData; -use std::ops::{Deref, DerefMut}; -use std::ptr::{NonNull, addr_of_mut, slice_from_raw_parts_mut}; - -/// A type that is functionally equivalent to `(Header, Box<[Item]>)`, -/// but all data is stored in one heap allocation and the pointer is thin, -/// so the whole thing's size is like a pointer. -pub struct ThinVecWithHeader { - /// INVARIANT: Points to a valid heap allocation that contains `ThinVecInner
`, - /// followed by (suitably aligned) `len` `Item`s. - ptr: NonNull>, - _marker: PhantomData<(Header, Box<[Item]>)>, -} - -// SAFETY: We essentially own both the header and the items. -unsafe impl Send for ThinVecWithHeader {} -unsafe impl Sync for ThinVecWithHeader {} - -#[derive(Clone)] -struct ThinVecInner
{ - header: Header, - len: usize, -} - -impl ThinVecWithHeader { - /// # Safety - /// - /// The iterator must produce `len` elements. - #[inline] - unsafe fn from_trusted_len_iter( - header: Header, - len: usize, - items: impl Iterator, - ) -> Self { - let (ptr, layout, items_offset) = Self::allocate(len); - - struct DeallocGuard(*mut u8, Layout); - impl Drop for DeallocGuard { - fn drop(&mut self) { - // SAFETY: We allocated this above. - unsafe { - dealloc(self.0, self.1); - } - } - } - let _dealloc_guard = DeallocGuard(ptr.as_ptr().cast::(), layout); - - // INVARIANT: Between `0..1` there are only initialized items. - struct ItemsGuard(*mut Item, *mut Item); - impl Drop for ItemsGuard { - fn drop(&mut self) { - // SAFETY: Our invariant. - unsafe { - slice_from_raw_parts_mut(self.0, self.1.offset_from(self.0) as usize) - .drop_in_place(); - } - } - } - - // SAFETY: We allocated enough space. - let mut items_ptr = unsafe { ptr.as_ptr().byte_add(items_offset).cast::() }; - // INVARIANT: There are zero elements in this range. - let mut items_guard = ItemsGuard(items_ptr, items_ptr); - items.for_each(|item| { - // SAFETY: Our precondition guarantee we won't get more than `len` items, and we allocated - // enough space for `len` items. - unsafe { - items_ptr.write(item); - items_ptr = items_ptr.add(1); - } - // INVARIANT: We just initialized this item. - items_guard.1 = items_ptr; - }); - - // SAFETY: We allocated enough space. - unsafe { - ptr.write(ThinVecInner { header, len }); - } - - std::mem::forget(items_guard); - - std::mem::forget(_dealloc_guard); - - // INVARIANT: We allocated and initialized all fields correctly. - Self { ptr, _marker: PhantomData } - } - - #[inline] - fn allocate(len: usize) -> (NonNull>, Layout, usize) { - let (layout, items_offset) = Self::layout(len); - // SAFETY: We always have `len`, so our allocation cannot be zero-sized. - let ptr = unsafe { std::alloc::alloc(layout).cast::>() }; - let Some(ptr) = NonNull::>::new(ptr) else { - handle_alloc_error(layout); - }; - (ptr, layout, items_offset) - } - - #[inline] - #[allow(clippy::should_implement_trait)] - pub fn from_iter(header: Header, items: I) -> Self - where - I: IntoIterator, - I::IntoIter: TrustedLen, - { - let items = items.into_iter(); - // SAFETY: `TrustedLen` guarantees the iterator length is exact. - unsafe { Self::from_trusted_len_iter(header, items.len(), items) } - } - - #[inline] - fn items_offset(&self) -> usize { - // SAFETY: We `pad_to_align()` in `layout()`, so at most where accessing past the end of the allocation, - // which is allowed. - unsafe { - Layout::new::>().extend(Layout::new::()).unwrap_unchecked().1 - } - } - - #[inline] - fn header_and_len(&self) -> &ThinVecInner
{ - // SAFETY: By `ptr`'s invariant, it is correctly allocated and initialized. - unsafe { &*self.ptr.as_ptr() } - } - - #[inline] - fn items_ptr(&self) -> *mut [Item] { - let len = self.header_and_len().len; - // SAFETY: `items_offset()` returns the correct offset of the items, where they are allocated. - let ptr = unsafe { self.ptr.as_ptr().byte_add(self.items_offset()).cast::() }; - slice_from_raw_parts_mut(ptr, len) - } - - #[inline] - pub fn header(&self) -> &Header { - &self.header_and_len().header - } - - #[inline] - pub fn header_mut(&mut self) -> &mut Header { - // SAFETY: By `ptr`'s invariant, it is correctly allocated and initialized. - unsafe { &mut *addr_of_mut!((*self.ptr.as_ptr()).header) } - } - - #[inline] - pub fn items(&self) -> &[Item] { - // SAFETY: `items_ptr()` gives a valid pointer. - unsafe { &*self.items_ptr() } - } - - #[inline] - pub fn items_mut(&mut self) -> &mut [Item] { - // SAFETY: `items_ptr()` gives a valid pointer. - unsafe { &mut *self.items_ptr() } - } - - #[inline] - pub fn len(&self) -> usize { - self.header_and_len().len - } - - #[inline] - fn layout(len: usize) -> (Layout, usize) { - let (layout, items_offset) = Layout::new::>() - .extend(Layout::array::(len).expect("too big `ThinVec` requested")) - .expect("too big `ThinVec` requested"); - let layout = layout.pad_to_align(); - (layout, items_offset) - } -} - -/// # Safety -/// -/// The length reported must be exactly the number of items yielded. -pub unsafe trait TrustedLen: ExactSizeIterator {} - -unsafe impl TrustedLen for std::vec::IntoIter {} -unsafe impl TrustedLen for std::slice::Iter<'_, T> {} -unsafe impl<'a, T: Clone + 'a, I: TrustedLen> TrustedLen for std::iter::Cloned {} -unsafe impl T> TrustedLen for std::iter::Map {} -unsafe impl TrustedLen for std::vec::Drain<'_, T> {} -unsafe impl TrustedLen for std::array::IntoIter {} - -impl Clone for ThinVecWithHeader { - #[inline] - fn clone(&self) -> Self { - Self::from_iter(self.header().clone(), self.items().iter().cloned()) - } -} - -impl Drop for ThinVecWithHeader { - #[inline] - fn drop(&mut self) { - // This must come before we drop `header`, because after that we cannot make a reference to it in `len()`. - let len = self.len(); - - // SAFETY: The contents are allocated and initialized. - unsafe { - addr_of_mut!((*self.ptr.as_ptr()).header).drop_in_place(); - self.items_ptr().drop_in_place(); - } - - let (layout, _) = Self::layout(len); - // SAFETY: This was allocated in `new()` with the same layout calculation. - unsafe { - dealloc(self.ptr.as_ptr().cast::(), layout); - } - } -} - -impl fmt::Debug for ThinVecWithHeader { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("ThinVecWithHeader") - .field("header", self.header()) - .field("items", &self.items()) - .finish() - } -} - -impl PartialEq for ThinVecWithHeader { - #[inline] - fn eq(&self, other: &Self) -> bool { - self.header() == other.header() && self.items() == other.items() - } -} - -impl Eq for ThinVecWithHeader {} - -impl Hash for ThinVecWithHeader { - #[inline] - fn hash(&self, state: &mut H) { - self.header().hash(state); - self.items().hash(state); - } -} - -#[derive(Clone, PartialEq, Eq, Hash)] -pub struct ThinVec(ThinVecWithHeader<(), T>); - -impl ThinVec { - #[inline] - #[allow(clippy::should_implement_trait)] - pub fn from_iter(values: I) -> Self - where - I: IntoIterator, - I::IntoIter: TrustedLen, - { - Self(ThinVecWithHeader::from_iter((), values)) - } - - #[inline] - pub fn len(&self) -> usize { - self.0.len() - } - - #[inline] - pub fn iter(&self) -> std::slice::Iter<'_, T> { - (**self).iter() - } - - #[inline] - pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> { - (**self).iter_mut() - } -} - -impl Deref for ThinVec { - type Target = [T]; - - #[inline] - fn deref(&self) -> &Self::Target { - self.0.items() - } -} - -impl DerefMut for ThinVec { - #[inline] - fn deref_mut(&mut self) -> &mut Self::Target { - self.0.items_mut() - } -} - -impl<'a, T> IntoIterator for &'a ThinVec { - type IntoIter = std::slice::Iter<'a, T>; - type Item = &'a T; - - #[inline] - fn into_iter(self) -> Self::IntoIter { - self.iter() - } -} - -impl<'a, T> IntoIterator for &'a mut ThinVec { - type IntoIter = std::slice::IterMut<'a, T>; - type Item = &'a mut T; - - #[inline] - fn into_iter(self) -> Self::IntoIter { - self.iter_mut() - } -} - -impl fmt::Debug for ThinVec { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_list().entries(&**self).finish() - } -} - -/// A [`ThinVec`] that requires no allocation for the empty case. -#[derive(Clone, PartialEq, Eq, Hash)] -pub struct EmptyOptimizedThinVec(Option>); - -impl EmptyOptimizedThinVec { - #[inline] - #[allow(clippy::should_implement_trait)] - pub fn from_iter(values: I) -> Self - where - I: IntoIterator, - I::IntoIter: TrustedLen, - { - let values = values.into_iter(); - if values.len() == 0 { Self::empty() } else { Self(Some(ThinVec::from_iter(values))) } - } - - #[inline] - pub fn empty() -> Self { - Self(None) - } - - #[inline] - pub fn len(&self) -> usize { - self.0.as_ref().map_or(0, ThinVec::len) - } - - #[inline] - pub fn iter(&self) -> std::slice::Iter<'_, T> { - (**self).iter() - } - - #[inline] - pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> { - (**self).iter_mut() - } -} - -impl Default for EmptyOptimizedThinVec { - #[inline] - fn default() -> Self { - Self::empty() - } -} - -impl Deref for EmptyOptimizedThinVec { - type Target = [T]; - - #[inline] - fn deref(&self) -> &Self::Target { - self.0.as_deref().unwrap_or_default() - } -} - -impl DerefMut for EmptyOptimizedThinVec { - #[inline] - fn deref_mut(&mut self) -> &mut Self::Target { - self.0.as_deref_mut().unwrap_or_default() - } -} - -impl<'a, T> IntoIterator for &'a EmptyOptimizedThinVec { - type IntoIter = std::slice::Iter<'a, T>; - type Item = &'a T; - - #[inline] - fn into_iter(self) -> Self::IntoIter { - self.iter() - } -} - -impl<'a, T> IntoIterator for &'a mut EmptyOptimizedThinVec { - type IntoIter = std::slice::IterMut<'a, T>; - type Item = &'a mut T; - - #[inline] - fn into_iter(self) -> Self::IntoIter { - self.iter_mut() - } -} - -impl fmt::Debug for EmptyOptimizedThinVec { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_list().entries(&**self).finish() - } -} - -/// Syntax: -/// -/// ```ignore -/// thin_vec_with_header_struct! { -/// pub new(pub(crate)) struct MyCoolStruct, MyCoolStructHeader { -/// pub(crate) variable_length: [Ty], -/// pub field1: CopyTy, -/// pub field2: NonCopyTy; ref, -/// } -/// } -/// ``` -#[doc(hidden)] -#[macro_export] -macro_rules! thin_vec_with_header_struct_ { - (@maybe_ref (ref) $($t:tt)*) => { &$($t)* }; - (@maybe_ref () $($t:tt)*) => { $($t)* }; - ( - $vis:vis new($new_vis:vis) struct $struct:ident, $header:ident { - $items_vis:vis $items:ident : [$items_ty:ty], - $( $header_var_vis:vis $header_var:ident : $header_var_ty:ty $(; $ref:ident)?, )+ - } - ) => { - #[derive(Debug, Clone, Eq, PartialEq, Hash)] - struct $header { - $( $header_var : $header_var_ty, )+ - } - - #[derive(Clone, Eq, PartialEq, Hash)] - $vis struct $struct($crate::thin_vec::ThinVecWithHeader<$header, $items_ty>); - - impl $struct { - #[inline] - #[allow(unused)] - $new_vis fn new( - $( $header_var: $header_var_ty, )+ - $items: I, - ) -> Self - where - I: ::std::iter::IntoIterator, - I::IntoIter: $crate::thin_vec::TrustedLen, - { - Self($crate::thin_vec::ThinVecWithHeader::from_iter( - $header { $( $header_var, )+ }, - $items, - )) - } - - #[inline] - $items_vis fn $items(&self) -> &[$items_ty] { - self.0.items() - } - - $( - #[inline] - $header_var_vis fn $header_var(&self) -> $crate::thin_vec_with_header_struct_!(@maybe_ref ($($ref)?) $header_var_ty) { - $crate::thin_vec_with_header_struct_!(@maybe_ref ($($ref)?) self.0.header().$header_var) - } - )+ - } - - impl ::std::fmt::Debug for $struct { - fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { - f.debug_struct(stringify!($struct)) - $( .field(stringify!($header_var), &self.$header_var()) )* - .field(stringify!($items), &self.$items()) - .finish() - } - } - }; -} -pub use crate::thin_vec_with_header_struct_ as thin_vec_with_header_struct;