Skip to content

Commit 07201cd

Browse files
committed
Avoid unecessary imports from alloc for prelude types
Types like `Vec` and `Box` are already in the std prelude, so don't need to be imported when cfg(feature = "std") is enabled.
1 parent 4af144d commit 07201cd

File tree

5 files changed

+15
-3
lines changed

5 files changed

+15
-3
lines changed

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ extern crate self as zerogc;
4545
* I want this library to use 'mostly' stable features,
4646
* unless there's good justification to use an unstable feature.
4747
*/
48+
#[cfg(all(not(feature = "std"), feature = "alloc"))]
49+
use alloc::vec::Vec;
4850
use core::cmp::Ordering;
4951
use core::fmt::{self, Debug, Display, Formatter};
5052
use core::hash::{Hash, Hasher};
@@ -524,7 +526,7 @@ pub unsafe trait GcSimpleAlloc: GcContext {
524526
/// Allocate an array, taking ownership of the values in
525527
/// the specified vec.
526528
#[inline]
527-
#[cfg(feature = "alloc")]
529+
#[cfg(any(feature = "alloc", feature = "std"))]
528530
fn alloc_array_from_vec<'gc, T>(&'gc self, mut src: Vec<T>) -> GcArray<'gc, T, Self::Id>
529531
where
530532
T: GcSafe<'gc, Self::Id>,

src/manually_traced/stdalloc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
//!
33
//! These can be used in `#![no_std]` crates without requiring
44
//! the entire standard library.
5+
#[cfg(not(feature = "std"))]
56
use alloc::boxed::Box;
67
use alloc::rc::Rc;
8+
#[cfg(not(feature = "std"))]
79
use alloc::string::String;
810
use alloc::sync::Arc;
11+
#[cfg(not(feature = "std"))]
912
use alloc::vec::Vec;
1013

1114
use crate::prelude::*;

src/vec.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@
103103
//! It has been called the [stretchy vector problem](https://www.ravenbrook.com/project/mps/master/manual/html/guide/vector.html)
104104
//! by some. This is less of a problem for `zerogc`, because collections can only
105105
//! happen at explicit safepoints.
106+
#[cfg(all(not(feature = "std"), feature = "alloc"))]
107+
use alloc::vec::Vec;
106108
use core::cell::UnsafeCell;
107-
use core::convert::{AsMut, AsRef};
108109
use core::fmt::{self, Debug, Formatter};
109110
use core::marker::PhantomData;
110111
use core::ops::{Deref, DerefMut, Index, IndexMut, RangeBounds};
@@ -383,6 +384,7 @@ unsafe impl<'gc, T: GcSafe<'gc, Id>, Id: CollectorId> IGcVec<'gc, T> for GcVec<'
383384
pub fn copy_from_slice(src: &[T], ctx: &'gc Id::Context) -> Self
384385
where
385386
T: Copy;
387+
#[cfg(any(feature = "alloc", feature = "std"))]
386388
pub fn from_vec(src: Vec<T>, ctx: &'gc Id::Context) -> Self;
387389

388390
/*

src/vec/cell.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! The implementation of [GcVecCell]
2+
#[cfg(all(not(feature = "std"), feature = "alloc"))]
3+
use alloc::vec::Vec;
24
use core::cell::RefCell;
35

46
use inherent::inherent;
@@ -135,6 +137,7 @@ unsafe impl<'gc, T: GcSafe<'gc, Id>, Id: SimpleAllocCollectorId> IGcVec<'gc, T>
135137
pub fn copy_from_slice(src: &[T], ctx: &'gc <Id as CollectorId>::Context) -> Self
136138
where
137139
T: Copy;
140+
#[cfg(feature = "alloc")]
138141
pub fn from_vec(src: Vec<T>, ctx: &'gc <Id as CollectorId>::Context) -> Self;
139142
pub fn get(&mut self, index: usize) -> Option<T>
140143
where

src/vec/raw.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! The underlying representation of a [GcVec](`crate::vec::GcVec`)
22
3-
use core::iter::Iterator;
3+
#[cfg(all(not(feature = "std"), feature = "alloc"))]
4+
use alloc::vec::Vec;
45
use core::marker::PhantomData;
56

67
use crate::{CollectorId, GcRebrand, GcSafe};
@@ -40,6 +41,7 @@ pub unsafe trait IGcVec<'gc, T: GcSafe<'gc, Self::Id>>: Sized + Extend<T> {
4041
///
4142
/// This consumes ownership of the original vector.
4243
#[inline]
44+
#[cfg(any(feature = "alloc", feature = "std"))]
4345
fn from_vec(mut src: Vec<T>, ctx: &'gc <Self::Id as CollectorId>::Context) -> Self {
4446
let len = src.len();
4547
let mut res = Self::with_capacity_in(len, ctx);

0 commit comments

Comments
 (0)