Skip to content

Commit 18e0839

Browse files
authored
Make metal dependency rely on the metal feature (#214)
* Make `metal` dependency rely on the `metal` feature We seem to have this weird case where the `metal` crate is always included (on supported target OSes), even if the non-default `metal` feature isn't turned on. Found this while wondering why `cargo` wasn't complaining about a clash thanks to the implicit feature created by an optional crate with an identically-named feature in the `[features]` table without [the `dep:` rename prefix], turns out that the `metal` crate wasn't `optional = true` in the first place. Fix that. [the `dep:` rename prefix]: https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies * Remove unnecessary `map_or()` (could also have been a `map()` or `inspect()`) Our MSRV is still at 1.65 otherwise we could use https://doc.rust-lang.org/std/option/enum.Option.html#method.inspect from Rust 1.76. * metal: Clean up repetitive import
1 parent 0b5d2cf commit 18e0839

File tree

4 files changed

+37
-47
lines changed

4 files changed

+37
-47
lines changed

Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ all-features = true
2626
log = "0.4"
2727
thiserror = "1.0"
2828
presser = { version = "0.3" }
29-
# Only needed for vulkan. Disable all default features as good practice,
29+
# Only needed for Vulkan. Disable all default features as good practice,
3030
# such as the ability to link/load a Vulkan library.
3131
ash = { version = ">=0.34, <=0.37", optional = true, default-features = false, features = ["debug"] }
3232
# Only needed for visualizer.
3333
egui = { version = ">=0.24, <=0.27", optional = true, default-features = false }
3434
egui_extras = { version = ">=0.24, <=0.27", optional = true, default-features = false }
3535

3636
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
37-
metal = { version = "0.27.0", default-features = false, features = ["link", "dispatch",] }
37+
metal = { version = "0.27.0", default-features = false, features = ["link", "dispatch"], optional = true }
3838

3939
[target.'cfg(windows)'.dependencies]
4040
# Only needed for public-winapi interop helpers
@@ -88,10 +88,10 @@ name = "metal-buffer"
8888
required-features = ["metal"]
8989

9090
[features]
91-
visualizer = ["egui", "egui_extras"]
92-
vulkan = ["ash"]
93-
d3d12 = ["windows"]
94-
metal = []
91+
visualizer = ["dep:egui", "dep:egui_extras"]
92+
vulkan = ["dep:ash"]
93+
d3d12 = ["dep:windows"]
94+
metal = ["dep:metal"]
9595
# Expose helper functionality for winapi types to interface with gpu-allocator, which is primarily windows-rs driven
9696
public-winapi = ["dep:winapi"]
9797

release.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ sign-tag = true
66
publish = false
77

88
pre-release-replacements = [
9-
{file="README.md", search="gpu-allocator = .*", replace="{{crate_name}} = \"{{version}}\""},
10-
{file="README.tpl", search="gpu-allocator = .*", replace="{{crate_name}} = \"{{version}}\""},
9+
{ file = "README.md", search = "gpu-allocator = .*", replace = "{{crate_name}} = \"{{version}}\"" },
10+
{ file = "README.tpl", search = "gpu-allocator = .*", replace = "{{crate_name}} = \"{{version}}\"" },
1111
]

src/metal/mod.rs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
use std::{backtrace::Backtrace, sync::Arc};
33

44
use crate::{
5-
allocator::{self, AllocationType},
6-
AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result,
5+
allocator, AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result,
76
};
87
use log::{debug, Level};
9-
use metal::MTLStorageMode;
108

119
fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptions {
1210
match location {
@@ -36,43 +34,34 @@ impl Allocation {
3634
let resource =
3735
self.heap
3836
.new_buffer_with_offset(self.size, self.heap.resource_options(), self.offset);
39-
resource.map_or_else(
40-
|| None,
41-
|resource| {
42-
if let Some(name) = &self.name {
43-
resource.set_label(name);
44-
}
45-
Some(resource)
46-
},
47-
)
37+
if let Some(resource) = &resource {
38+
if let Some(name) = &self.name {
39+
resource.set_label(name);
40+
}
41+
}
42+
resource
4843
}
4944

5045
pub fn make_texture(&self, desc: &metal::TextureDescriptor) -> Option<metal::Texture> {
5146
let resource = self.heap.new_texture_with_offset(desc, self.offset);
52-
resource.map_or_else(
53-
|| None,
54-
|resource| {
55-
if let Some(name) = &self.name {
56-
resource.set_label(name);
57-
}
58-
Some(resource)
59-
},
60-
)
47+
if let Some(resource) = &resource {
48+
if let Some(name) = &self.name {
49+
resource.set_label(name);
50+
}
51+
}
52+
resource
6153
}
6254

6355
pub fn make_acceleration_structure(&self) -> Option<metal::AccelerationStructure> {
6456
let resource = self
6557
.heap
6658
.new_acceleration_structure_with_size_offset(self.size, self.offset);
67-
resource.map_or_else(
68-
|| None,
69-
|resource| {
70-
if let Some(name) = &self.name {
71-
resource.set_label(name);
72-
}
73-
Some(resource)
74-
},
75-
)
59+
if let Some(resource) = &resource {
60+
if let Some(name) = &self.name {
61+
resource.set_label(name);
62+
}
63+
}
64+
resource
7665
}
7766

7867
fn is_null(&self) -> bool {
@@ -115,10 +104,10 @@ impl<'a> AllocationCreateDesc<'a> {
115104
Self {
116105
name,
117106
location: match desc.storage_mode() {
118-
MTLStorageMode::Shared | MTLStorageMode::Managed | MTLStorageMode::Memoryless => {
119-
MemoryLocation::Unknown
120-
}
121-
MTLStorageMode::Private => MemoryLocation::GpuOnly,
107+
metal::MTLStorageMode::Shared
108+
| metal::MTLStorageMode::Managed
109+
| metal::MTLStorageMode::Memoryless => MemoryLocation::Unknown,
110+
metal::MTLStorageMode::Private => MemoryLocation::GpuOnly,
122111
},
123112
size: size_and_align.size,
124113
alignment: size_and_align.align,
@@ -203,9 +192,10 @@ impl MemoryType {
203192
backtrace: Arc<Backtrace>,
204193
allocation_sizes: &AllocationSizes,
205194
) -> Result<Allocation> {
206-
let allocation_type = AllocationType::Linear;
195+
let allocation_type = allocator::AllocationType::Linear;
207196

208-
let memblock_size = if self.heap_properties.storage_mode() == MTLStorageMode::Private {
197+
let memblock_size = if self.heap_properties.storage_mode() == metal::MTLStorageMode::Private
198+
{
209199
allocation_sizes.device_memblock_size
210200
} else {
211201
allocation_sizes.host_memblock_size

src/vulkan/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{backtrace::Backtrace, fmt, marker::PhantomData, sync::Arc};
1010
use ash::vk;
1111
use log::{debug, Level};
1212

13-
use super::allocator::{self, AllocationType};
13+
use super::allocator;
1414
use crate::{
1515
allocator::fmt_bytes, AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation,
1616
Result,
@@ -459,9 +459,9 @@ impl MemoryType {
459459
allocation_sizes: &AllocationSizes,
460460
) -> Result<Allocation> {
461461
let allocation_type = if desc.linear {
462-
AllocationType::Linear
462+
allocator::AllocationType::Linear
463463
} else {
464-
AllocationType::NonLinear
464+
allocator::AllocationType::NonLinear
465465
};
466466

467467
let memblock_size = if self

0 commit comments

Comments
 (0)