Skip to content

Commit a22352a

Browse files
committed
chore: replace std::* with core::* or alloc::*
1 parent a38cde4 commit a22352a

File tree

7 files changed

+68
-53
lines changed

7 files changed

+68
-53
lines changed

src/allocator/dedicated_block_allocator/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ use std::{backtrace::Backtrace, sync::Arc};
44

55
#[cfg(feature = "visualizer")]
66
pub(crate) mod visualizer;
7-
8-
use std::{backtrace::Backtrace, sync::Arc};
7+
use alloc::{
8+
borrow::ToOwned,
9+
string::{String, ToString},
10+
vec::Vec,
11+
};
912

1013
use log::{log, Level};
1114

@@ -63,12 +66,12 @@ impl SubAllocator for DedicatedBlockAllocator {
6366
}
6467

6568
#[allow(clippy::unwrap_used)]
66-
let dummy_id = std::num::NonZeroU64::new(1).unwrap();
69+
let dummy_id = core::num::NonZeroU64::new(1).unwrap();
6770
Ok((0, dummy_id))
6871
}
6972

70-
fn free(&mut self, chunk_id: Option<std::num::NonZeroU64>) -> Result<()> {
71-
if chunk_id != std::num::NonZeroU64::new(1) {
73+
fn free(&mut self, chunk_id: Option<core::num::NonZeroU64>) -> Result<()> {
74+
if chunk_id != core::num::NonZeroU64::new(1) {
7275
Err(AllocationError::Internal("Chunk ID must be 1.".into()))
7376
} else {
7477
self.allocated = 0;
@@ -78,10 +81,10 @@ impl SubAllocator for DedicatedBlockAllocator {
7881

7982
fn rename_allocation(
8083
&mut self,
81-
chunk_id: Option<std::num::NonZeroU64>,
84+
chunk_id: Option<core::num::NonZeroU64>,
8285
name: &str,
8386
) -> Result<()> {
84-
if chunk_id != std::num::NonZeroU64::new(1) {
87+
if chunk_id != core::num::NonZeroU64::new(1) {
8588
Err(AllocationError::Internal("Chunk ID must be 1.".into()))
8689
} else {
8790
self.name = Some(name.into());

src/allocator/free_list_allocator/mod.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ use std::{
1010
sync::Arc,
1111
};
1212

13+
use alloc::{
14+
borrow::ToOwned,
15+
string::{String, ToString},
16+
vec::Vec,
17+
};
18+
19+
#[cfg(feature = "hashbrown")]
20+
use hashbrown::{HashMap, HashSet};
21+
1322
use log::{log, Level};
1423

1524
use super::{AllocationReport, AllocationType, SubAllocator, SubAllocatorBase};
@@ -27,25 +36,25 @@ fn align_up(val: u64, alignment: u64) -> u64 {
2736

2837
#[derive(Debug)]
2938
pub(crate) struct MemoryChunk {
30-
pub(crate) chunk_id: std::num::NonZeroU64,
39+
pub(crate) chunk_id: core::num::NonZeroU64,
3140
pub(crate) size: u64,
3241
pub(crate) offset: u64,
3342
pub(crate) allocation_type: AllocationType,
3443
pub(crate) name: Option<String>,
3544
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
3645
#[cfg(feature = "std")]
3746
pub(crate) backtrace: Arc<Backtrace>,
38-
next: Option<std::num::NonZeroU64>,
39-
prev: Option<std::num::NonZeroU64>,
47+
next: Option<core::num::NonZeroU64>,
48+
prev: Option<core::num::NonZeroU64>,
4049
}
4150

4251
#[derive(Debug)]
4352
pub(crate) struct FreeListAllocator {
4453
size: u64,
4554
allocated: u64,
4655
pub(crate) chunk_id_counter: u64,
47-
pub(crate) chunks: HashMap<std::num::NonZeroU64, MemoryChunk>,
48-
free_chunks: HashSet<std::num::NonZeroU64>,
56+
pub(crate) chunks: HashMap<core::num::NonZeroU64, MemoryChunk>,
57+
free_chunks: HashSet<core::num::NonZeroU64>,
4958
}
5059

5160
/// Test if two suballocations will overlap the same page.
@@ -70,7 +79,7 @@ fn has_granularity_conflict(type0: AllocationType, type1: AllocationType) -> boo
7079
impl FreeListAllocator {
7180
pub(crate) fn new(size: u64) -> Self {
7281
#[allow(clippy::unwrap_used)]
73-
let initial_chunk_id = std::num::NonZeroU64::new(1).unwrap();
82+
let initial_chunk_id = core::num::NonZeroU64::new(1).unwrap();
7483

7584
let mut chunks = HashMap::default();
7685
chunks.insert(
@@ -103,27 +112,27 @@ impl FreeListAllocator {
103112
}
104113

105114
/// Generates a new unique chunk ID
106-
fn get_new_chunk_id(&mut self) -> Result<std::num::NonZeroU64> {
115+
fn get_new_chunk_id(&mut self) -> Result<core::num::NonZeroU64> {
107116
if self.chunk_id_counter == u64::MAX {
108117
// End of chunk id counter reached, no more allocations are possible.
109118
return Err(AllocationError::OutOfMemory);
110119
}
111120

112121
let id = self.chunk_id_counter;
113122
self.chunk_id_counter += 1;
114-
std::num::NonZeroU64::new(id).ok_or_else(|| {
123+
core::num::NonZeroU64::new(id).ok_or_else(|| {
115124
AllocationError::Internal("New chunk id was 0, which is not allowed.".into())
116125
})
117126
}
118127
/// Finds the specified `chunk_id` in the list of free chunks and removes if from the list
119-
fn remove_id_from_free_list(&mut self, chunk_id: std::num::NonZeroU64) {
128+
fn remove_id_from_free_list(&mut self, chunk_id: core::num::NonZeroU64) {
120129
self.free_chunks.remove(&chunk_id);
121130
}
122131
/// Merges two adjacent chunks. Right chunk will be merged into the left chunk
123132
fn merge_free_chunks(
124133
&mut self,
125-
chunk_left: std::num::NonZeroU64,
126-
chunk_right: std::num::NonZeroU64,
134+
chunk_left: core::num::NonZeroU64,
135+
chunk_right: core::num::NonZeroU64,
127136
) -> Result<()> {
128137
// Gather data from right chunk and remove it
129138
let (right_size, right_next) = {
@@ -172,7 +181,7 @@ impl SubAllocator for FreeListAllocator {
172181
return Err(AllocationError::OutOfMemory);
173182
}
174183

175-
let mut best_fit_id: Option<std::num::NonZeroU64> = None;
184+
let mut best_fit_id: Option<core::num::NonZeroU64> = None;
176185
let mut best_offset = 0u64;
177186
let mut best_aligned_size = 0u64;
178187
let mut best_chunk_size = 0u64;
@@ -297,7 +306,7 @@ impl SubAllocator for FreeListAllocator {
297306
Ok((best_offset, chunk_id))
298307
}
299308

300-
fn free(&mut self, chunk_id: Option<std::num::NonZeroU64>) -> Result<()> {
309+
fn free(&mut self, chunk_id: Option<core::num::NonZeroU64>) -> Result<()> {
301310
let chunk_id = chunk_id
302311
.ok_or_else(|| AllocationError::Internal("Chunk ID must be a valid value.".into()))?;
303312

@@ -337,7 +346,7 @@ impl SubAllocator for FreeListAllocator {
337346

338347
fn rename_allocation(
339348
&mut self,
340-
chunk_id: Option<std::num::NonZeroU64>,
349+
chunk_id: Option<core::num::NonZeroU64>,
341350
name: &str,
342351
) -> Result<()> {
343352
let chunk_id = chunk_id

src/allocator/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::{backtrace::Backtrace, fmt, ops::Range, sync::Arc};
2-
1+
use alloc::{fmt, string::String, vec::Vec};
2+
use core::ops::Range;
33
use log::*;
44
#[cfg(feature = "std")]
55
use std::{backtrace::Backtrace, sync::Arc};
@@ -81,15 +81,15 @@ impl fmt::Debug for AllocationReport {
8181
impl fmt::Debug for AllocatorReport {
8282
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8383
let mut allocations = self.allocations.clone();
84-
allocations.sort_by_key(|alloc| std::cmp::Reverse(alloc.size));
84+
allocations.sort_by_key(|alloc| core::cmp::Reverse(alloc.size));
8585

8686
let max_num_allocations_to_print = f.precision().unwrap_or(usize::MAX);
8787
allocations.truncate(max_num_allocations_to_print);
8888

8989
f.debug_struct("AllocatorReport")
9090
.field(
9191
"summary",
92-
&std::format_args!(
92+
&core::format_args!(
9393
"{} / {}",
9494
fmt_bytes(self.total_allocated_bytes),
9595
fmt_bytes(self.total_reserved_bytes)
@@ -118,11 +118,11 @@ pub(crate) trait SubAllocator: SubAllocatorBase + fmt::Debug + Sync + Send {
118118
#[cfg(feature = "std")] backtrace: Arc<Backtrace>,
119119
) -> Result<(u64, core::num::NonZeroU64)>;
120120

121-
fn free(&mut self, chunk_id: Option<std::num::NonZeroU64>) -> Result<()>;
121+
fn free(&mut self, chunk_id: Option<core::num::NonZeroU64>) -> Result<()>;
122122

123123
fn rename_allocation(
124124
&mut self,
125-
chunk_id: Option<std::num::NonZeroU64>,
125+
chunk_id: Option<core::num::NonZeroU64>,
126126
name: &str,
127127
) -> Result<()>;
128128

src/d3d12/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,49 +38,49 @@ mod public_winapi {
3838

3939
impl ToWinapi<winapi_d3d12::ID3D12Resource> for ID3D12Resource {
4040
fn as_winapi(&self) -> *const winapi_d3d12::ID3D12Resource {
41-
unsafe { std::mem::transmute_copy(self) }
41+
unsafe { core::mem::transmute_copy(self) }
4242
}
4343

4444
fn as_winapi_mut(&mut self) -> *mut winapi_d3d12::ID3D12Resource {
45-
unsafe { std::mem::transmute_copy(self) }
45+
unsafe { core::mem::transmute_copy(self) }
4646
}
4747
}
4848

4949
impl ToWinapi<winapi_d3d12::ID3D12Device> for ID3D12Device {
5050
fn as_winapi(&self) -> *const winapi_d3d12::ID3D12Device {
51-
unsafe { std::mem::transmute_copy(self) }
51+
unsafe { core::mem::transmute_copy(self) }
5252
}
5353

5454
fn as_winapi_mut(&mut self) -> *mut winapi_d3d12::ID3D12Device {
55-
unsafe { std::mem::transmute_copy(self) }
55+
unsafe { core::mem::transmute_copy(self) }
5656
}
5757
}
5858

5959
impl ToWindows<ID3D12Device> for *const winapi_d3d12::ID3D12Device {
6060
fn as_windows(&self) -> &ID3D12Device {
61-
unsafe { std::mem::transmute(self) }
61+
unsafe { core::mem::transmute(self) }
6262
}
6363
}
6464

6565
impl ToWindows<ID3D12Device> for *mut winapi_d3d12::ID3D12Device {
6666
fn as_windows(&self) -> &ID3D12Device {
67-
unsafe { std::mem::transmute(self) }
67+
unsafe { core::mem::transmute(self) }
6868
}
6969
}
7070

7171
impl ToWindows<ID3D12Device> for &mut winapi_d3d12::ID3D12Device {
7272
fn as_windows(&self) -> &ID3D12Device {
73-
unsafe { std::mem::transmute(self) }
73+
unsafe { core::mem::transmute(self) }
7474
}
7575
}
7676

7777
impl ToWinapi<winapi_d3d12::ID3D12Heap> for ID3D12Heap {
7878
fn as_winapi(&self) -> *const winapi_d3d12::ID3D12Heap {
79-
unsafe { std::mem::transmute_copy(self) }
79+
unsafe { core::mem::transmute_copy(self) }
8080
}
8181

8282
fn as_winapi_mut(&mut self) -> *mut winapi_d3d12::ID3D12Heap {
83-
unsafe { std::mem::transmute_copy(self) }
83+
unsafe { core::mem::transmute_copy(self) }
8484
}
8585
}
8686
}
@@ -208,10 +208,10 @@ impl<'a> AllocationCreateDesc<'a> {
208208
let device = device.as_windows();
209209
// Raw structs are binary-compatible
210210
let desc = unsafe {
211-
std::mem::transmute::<&winapi_d3d12::D3D12_RESOURCE_DESC, &D3D12_RESOURCE_DESC>(desc)
211+
core::mem::transmute::<&winapi_d3d12::D3D12_RESOURCE_DESC, &D3D12_RESOURCE_DESC>(desc)
212212
};
213213
let allocation_info =
214-
unsafe { device.GetResourceAllocationInfo(0, std::slice::from_ref(desc)) };
214+
unsafe { device.GetResourceAllocationInfo(0, core::slice::from_ref(desc)) };
215215
let resource_category: ResourceCategory = desc.into();
216216

217217
AllocationCreateDesc {
@@ -234,7 +234,7 @@ impl<'a> AllocationCreateDesc<'a> {
234234
location: MemoryLocation,
235235
) -> Self {
236236
let allocation_info =
237-
unsafe { device.GetResourceAllocationInfo(0, std::slice::from_ref(desc)) };
237+
unsafe { device.GetResourceAllocationInfo(0, core::slice::from_ref(desc)) };
238238
let resource_category: ResourceCategory = desc.into();
239239

240240
AllocationCreateDesc {
@@ -259,7 +259,7 @@ pub enum ID3D12DeviceVersion {
259259
Device12(ID3D12Device12),
260260
}
261261

262-
impl std::ops::Deref for ID3D12DeviceVersion {
262+
impl core::ops::Deref for ID3D12DeviceVersion {
263263
type Target = ID3D12Device;
264264

265265
fn deref(&self) -> &Self::Target {
@@ -324,7 +324,7 @@ pub struct CommittedAllocationStatistics {
324324

325325
#[derive(Debug)]
326326
pub struct Allocation {
327-
chunk_id: Option<std::num::NonZeroU64>,
327+
chunk_id: Option<core::num::NonZeroU64>,
328328
offset: u64,
329329
size: u64,
330330
memory_block_index: usize,
@@ -335,7 +335,7 @@ pub struct Allocation {
335335
}
336336

337337
impl Allocation {
338-
pub fn chunk_id(&self) -> Option<std::num::NonZeroU64> {
338+
pub fn chunk_id(&self) -> Option<core::num::NonZeroU64> {
339339
self.chunk_id
340340
}
341341

src/metal/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[cfg(feature = "std")]
22
use std::{backtrace::Backtrace, sync::Arc};
33

4+
use alloc::{boxed::Box, string::ToString, vec::Vec};
5+
46
#[cfg(feature = "visualizer")]
57
mod visualizer;
68
#[cfg(feature = "visualizer")]
@@ -30,7 +32,7 @@ fn memory_location_to_metal(location: MemoryLocation) -> MTLResourceOptions {
3032

3133
#[derive(Debug)]
3234
pub struct Allocation {
33-
chunk_id: Option<std::num::NonZeroU64>,
35+
chunk_id: Option<core::num::NonZeroU64>,
3436
offset: u64,
3537
size: u64,
3638
memory_block_index: usize,

src/result.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloc::string::String;
12
use thiserror::Error;
23

34
#[derive(Error, Debug)]
@@ -22,4 +23,4 @@ pub enum AllocationError {
2223
CastableFormatsRequiresAtLeastDevice12,
2324
}
2425

25-
pub type Result<V, E = AllocationError> = ::std::result::Result<V, E>;
26+
pub type Result<V, E = AllocationError> = ::core::result::Result<V, E>;

0 commit comments

Comments
 (0)