metal-rust 1.0.0

Safe Rust interfaces for Apple Metal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Safe public Metal 4 compiler factories and completion handlers.

use crate::Error;
use crate::metal::{
    BinaryArchive, ComputePipelineState, DynamicLibrary, Library, RenderPipelineState,
    SamplePosition, Size,
};
use crate::metal4::{
    BinaryFunction, BinaryFunctionDescriptor, CommandAllocator, Compiler, CompilerTask,
    CompilerTaskOptions, ComputePipelineDescriptor, FunctionDescriptor, LibraryDescriptor,
    MachineLearningPipelineDescriptor, MachineLearningPipelineState, MeshRenderPipelineDescriptor,
    PipelineDataSetSerializer, PipelineStageDynamicLinkingDescriptor, RenderPassDescriptor,
    RenderPipelineBinaryFunctionsDescriptor, RenderPipelineColorAttachmentDescriptor,
    RenderPipelineColorAttachmentDescriptorArray, RenderPipelineDescriptor,
    RenderPipelineDynamicLinkingDescriptor, StaticLinkingDescriptor, StitchedFunctionDescriptor,
    TileRenderPipelineDescriptor,
};
use std::collections::HashMap;
use std::path::Path;

/// A borrowed descriptor for any render, mesh, or tile pipeline build.
#[derive(Clone, Copy)]
pub enum RenderPipelineBuildDescriptor<'a> {
    /// A conventional vertex/fragment render pipeline.
    Render(&'a RenderPipelineDescriptor),
    /// A mesh/object render pipeline.
    Mesh(&'a MeshRenderPipelineDescriptor),
    /// A tile render pipeline.
    Tile(&'a TileRenderPipelineDescriptor),
}

impl<'a> RenderPipelineBuildDescriptor<'a> {
    fn to_ffi(self) -> metal_rust_ffi::RenderPipelineBuildDescriptor<'a> {
        match self {
            Self::Render(value) => {
                metal_rust_ffi::RenderPipelineBuildDescriptor::Render(&value.inner)
            }
            Self::Mesh(value) => metal_rust_ffi::RenderPipelineBuildDescriptor::Mesh(&value.inner),
            Self::Tile(value) => metal_rust_ffi::RenderPipelineBuildDescriptor::Tile(&value.inner),
        }
    }
}

impl Compiler {
    /// Creates a Metal library synchronously.
    pub fn new_library(&self, descriptor: &LibraryDescriptor) -> Result<Library, Error> {
        self.inner
            .new_library(&descriptor.inner)
            .map(Library::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Creates a compute pipeline with optional dynamic-linking and task options.
    pub fn new_compute_pipeline(
        &self,
        descriptor: &ComputePipelineDescriptor,
        linking: Option<&PipelineStageDynamicLinkingDescriptor>,
        options: Option<&CompilerTaskOptions>,
    ) -> Result<ComputePipelineState, Error> {
        self.inner
            .new_compute_pipeline(
                &descriptor.inner,
                linking.map(|value| &value.inner),
                options.map(|value| &value.inner),
            )
            .map(|inner| ComputePipelineState { inner })
            .map_err(Error::from_ffi)
    }

    /// Creates a render pipeline with optional dynamic-linking and task options.
    pub fn new_render_pipeline(
        &self,
        descriptor: RenderPipelineBuildDescriptor<'_>,
        linking: Option<&RenderPipelineDynamicLinkingDescriptor>,
        options: Option<&CompilerTaskOptions>,
    ) -> Result<RenderPipelineState, Error> {
        self.inner
            .new_render_pipeline(
                descriptor.to_ffi(),
                linking.map(|value| &value.inner),
                options.map(|value| &value.inner),
            )
            .map(|inner| RenderPipelineState { inner })
            .map_err(Error::from_ffi)
    }

    /// Specializes a previously unspecialized render pipeline.
    pub fn specialize_render_pipeline(
        &self,
        descriptor: RenderPipelineBuildDescriptor<'_>,
        pipeline: &RenderPipelineState,
    ) -> Result<RenderPipelineState, Error> {
        self.inner
            .specialize_render_pipeline(descriptor.to_ffi(), &pipeline.inner)
            .map(|inner| RenderPipelineState { inner })
            .map_err(Error::from_ffi)
    }

    /// Creates a binary shader function synchronously.
    pub fn new_binary_function(
        &self,
        descriptor: &BinaryFunctionDescriptor,
        options: Option<&CompilerTaskOptions>,
    ) -> Result<BinaryFunction, Error> {
        self.inner
            .new_binary_function(&descriptor.inner, options.map(|value| &value.inner))
            .map(BinaryFunction::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Creates a machine-learning pipeline synchronously.
    pub fn new_machine_learning_pipeline(
        &self,
        descriptor: &MachineLearningPipelineDescriptor,
    ) -> Result<MachineLearningPipelineState, Error> {
        self.inner
            .new_machine_learning_pipeline(&descriptor.inner)
            .map(MachineLearningPipelineState::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Creates a dynamic library from a compiled Metal library.
    pub fn new_dynamic_library(&self, library: &Library) -> Result<DynamicLibrary, Error> {
        self.inner
            .new_dynamic_library(&library.inner)
            .map(DynamicLibrary::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Creates a dynamic library from a file path.
    pub fn new_dynamic_library_from_path(
        &self,
        path: impl AsRef<Path>,
    ) -> Result<DynamicLibrary, Error> {
        self.inner
            .new_dynamic_library_from_path(path)
            .map(DynamicLibrary::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous Metal library compilation.
    pub fn new_library_async(
        &self,
        descriptor: &LibraryDescriptor,
        handler: impl FnOnce(Result<Library, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_library_async(&descriptor.inner, move |result| {
                handler(result.map(Library::from_ffi).map_err(Error::from_ffi));
            })
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous compute-pipeline compilation.
    pub fn new_compute_pipeline_async(
        &self,
        descriptor: &ComputePipelineDescriptor,
        options: Option<&CompilerTaskOptions>,
        handler: impl FnOnce(Result<ComputePipelineState, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_compute_pipeline_async(
                &descriptor.inner,
                options.map(|value| &value.inner),
                move |result| {
                    handler(
                        result
                            .map(|inner| ComputePipelineState { inner })
                            .map_err(Error::from_ffi),
                    );
                },
            )
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous compute compilation with dynamic linking.
    pub fn new_compute_pipeline_linked_async(
        &self,
        descriptor: &ComputePipelineDescriptor,
        linking: Option<&PipelineStageDynamicLinkingDescriptor>,
        options: Option<&CompilerTaskOptions>,
        handler: impl FnOnce(Result<ComputePipelineState, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_compute_pipeline_linked_async(
                &descriptor.inner,
                linking.map(|value| &value.inner),
                options.map(|value| &value.inner),
                move |result| {
                    handler(
                        result
                            .map(|inner| ComputePipelineState { inner })
                            .map_err(Error::from_ffi),
                    );
                },
            )
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous render-pipeline compilation.
    pub fn new_render_pipeline_async(
        &self,
        descriptor: RenderPipelineBuildDescriptor<'_>,
        options: Option<&CompilerTaskOptions>,
        handler: impl FnOnce(Result<RenderPipelineState, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_render_pipeline_async(
                descriptor.to_ffi(),
                options.map(|value| &value.inner),
                move |result| {
                    handler(
                        result
                            .map(|inner| RenderPipelineState { inner })
                            .map_err(Error::from_ffi),
                    );
                },
            )
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous render compilation with dynamic linking.
    pub fn new_render_pipeline_linked_async(
        &self,
        descriptor: RenderPipelineBuildDescriptor<'_>,
        linking: Option<&RenderPipelineDynamicLinkingDescriptor>,
        options: Option<&CompilerTaskOptions>,
        handler: impl FnOnce(Result<RenderPipelineState, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_render_pipeline_linked_async(
                descriptor.to_ffi(),
                linking.map(|value| &value.inner),
                options.map(|value| &value.inner),
                move |result| {
                    handler(
                        result
                            .map(|inner| RenderPipelineState { inner })
                            .map_err(Error::from_ffi),
                    );
                },
            )
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous specialization of a render pipeline.
    pub fn specialize_render_pipeline_async(
        &self,
        descriptor: RenderPipelineBuildDescriptor<'_>,
        pipeline: &RenderPipelineState,
        handler: impl FnOnce(Result<RenderPipelineState, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .specialize_render_pipeline_async(descriptor.to_ffi(), &pipeline.inner, move |result| {
                handler(
                    result
                        .map(|inner| RenderPipelineState { inner })
                        .map_err(Error::from_ffi),
                );
            })
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous binary-function compilation.
    pub fn new_binary_function_async(
        &self,
        descriptor: &BinaryFunctionDescriptor,
        options: Option<&CompilerTaskOptions>,
        handler: impl FnOnce(Result<BinaryFunction, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_binary_function_async(
                &descriptor.inner,
                options.map(|value| &value.inner),
                move |result| {
                    handler(
                        result
                            .map(BinaryFunction::from_ffi)
                            .map_err(Error::from_ffi),
                    );
                },
            )
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous machine-learning pipeline compilation.
    pub fn new_machine_learning_pipeline_async(
        &self,
        descriptor: &MachineLearningPipelineDescriptor,
        handler: impl FnOnce(Result<MachineLearningPipelineState, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_machine_learning_pipeline_async(&descriptor.inner, move |result| {
                handler(
                    result
                        .map(MachineLearningPipelineState::from_ffi)
                        .map_err(Error::from_ffi),
                );
            })
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous dynamic-library compilation from a library.
    pub fn new_dynamic_library_async(
        &self,
        library: &Library,
        handler: impl FnOnce(Result<DynamicLibrary, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_dynamic_library_async(&library.inner, move |result| {
                handler(
                    result
                        .map(DynamicLibrary::from_ffi)
                        .map_err(Error::from_ffi),
                );
            })
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Starts one-shot asynchronous dynamic-library compilation from a file path.
    pub fn new_dynamic_library_from_path_async(
        &self,
        path: impl AsRef<Path>,
        handler: impl FnOnce(Result<DynamicLibrary, Error>) + Send + 'static,
    ) -> Result<CompilerTask, Error> {
        self.inner
            .new_dynamic_library_from_path_async(path, move |result| {
                handler(
                    result
                        .map(DynamicLibrary::from_ffi)
                        .map_err(Error::from_ffi),
                );
            })
            .map(CompilerTask::from_ffi)
            .map_err(Error::from_ffi)
    }
}

impl CompilerTaskOptions {
    /// Returns binary archives searched by this compilation task.
    pub fn lookup_archives_vec(&self) -> Result<Vec<BinaryArchive>, Error> {
        self.inner
            .lookup_archives_vec()
            .map(|values| values.into_iter().map(BinaryArchive::from_ffi).collect())
            .map_err(Error::from_ffi)
    }

    /// Replaces binary archives searched by this compilation task.
    pub fn set_lookup_archives_slice(&self, values: &[BinaryArchive]) -> Result<(), Error> {
        let values: Vec<_> = values.iter().map(|value| value.inner.clone()).collect();
        self.inner
            .set_lookup_archives_slice(&values)
            .map_err(Error::from_ffi)
    }
}

impl StaticLinkingDescriptor {
    /// Returns public statically linked functions as owned safe wrappers.
    pub fn function_descriptors_vec(&self) -> Result<Vec<FunctionDescriptor>, Error> {
        self.inner
            .function_descriptors_vec()
            .map(|values| {
                values
                    .into_iter()
                    .map(FunctionDescriptor::from_ffi)
                    .collect()
            })
            .map_err(Error::from_ffi)
    }

    /// Replaces public statically linked functions from a borrowed slice.
    pub fn set_function_descriptors_slice(
        &self,
        values: &[FunctionDescriptor],
    ) -> Result<(), Error> {
        let values: Vec<_> = values.iter().map(|value| value.inner.clone()).collect();
        self.inner
            .set_function_descriptors_slice(&values)
            .map_err(Error::from_ffi)
    }

    /// Returns private statically linked functions as owned safe wrappers.
    pub fn internal_functions(&self) -> Result<Vec<FunctionDescriptor>, Error> {
        self.inner
            .internal_functions()
            .map(|values| {
                values
                    .into_iter()
                    .map(FunctionDescriptor::from_ffi)
                    .collect()
            })
            .map_err(Error::from_ffi)
    }

    /// Replaces private statically linked functions from a borrowed slice.
    pub fn set_internal_functions(&self, values: &[FunctionDescriptor]) -> Result<(), Error> {
        let values: Vec<_> = values.iter().map(|value| value.inner.clone()).collect();
        self.inner
            .set_internal_functions(&values)
            .map_err(Error::from_ffi)
    }

    /// Returns named static-link groups as Rust-native collections.
    pub fn groups_map(&self) -> Result<HashMap<String, Vec<FunctionDescriptor>>, Error> {
        self.inner
            .groups_map()
            .map(|groups| {
                groups
                    .into_iter()
                    .map(|(name, values)| {
                        (
                            name,
                            values
                                .into_iter()
                                .map(FunctionDescriptor::from_ffi)
                                .collect(),
                        )
                    })
                    .collect()
            })
            .map_err(Error::from_ffi)
    }

    /// Replaces named static-link groups from Rust-native collections.
    pub fn set_groups_map(
        &self,
        groups: &HashMap<String, Vec<FunctionDescriptor>>,
    ) -> Result<(), Error> {
        let groups = groups
            .iter()
            .map(|(name, values)| {
                (
                    name.clone(),
                    values.iter().map(|value| value.inner.clone()).collect(),
                )
            })
            .collect();
        self.inner.set_groups_map(&groups).map_err(Error::from_ffi)
    }
}

impl PipelineStageDynamicLinkingDescriptor {
    /// Returns binary functions selected for dynamic linking.
    pub fn binary_linked_functions_vec(&self) -> Result<Vec<BinaryFunction>, Error> {
        self.inner
            .binary_linked_functions_vec()
            .map(|values| values.into_iter().map(BinaryFunction::from_ffi).collect())
            .map_err(Error::from_ffi)
    }

    /// Replaces binary functions selected for dynamic linking.
    pub fn set_binary_linked_functions_slice(
        &self,
        values: &[BinaryFunction],
    ) -> Result<(), Error> {
        let values: Vec<_> = values.iter().map(|value| value.inner.clone()).collect();
        self.inner
            .set_binary_linked_functions_slice(&values)
            .map_err(Error::from_ffi)
    }

    /// Returns dynamic libraries preloaded during pipeline linking.
    pub fn preloaded_libraries_vec(&self) -> Result<Vec<DynamicLibrary>, Error> {
        self.inner
            .preloaded_libraries_vec()
            .map(|values| values.into_iter().map(DynamicLibrary::from_ffi).collect())
            .map_err(Error::from_ffi)
    }

    /// Replaces dynamic libraries preloaded during pipeline linking.
    pub fn set_preloaded_libraries_slice(&self, values: &[DynamicLibrary]) -> Result<(), Error> {
        let values: Vec<_> = values.iter().map(|value| value.inner.clone()).collect();
        self.inner
            .set_preloaded_libraries_slice(&values)
            .map_err(Error::from_ffi)
    }
}

macro_rules! facade_binary_function_list {
    ($getter:ident, $setter:ident) => {
        /// Returns this pipeline-stage binary-function list.
        pub fn $getter(&self) -> Result<Vec<BinaryFunction>, Error> {
            self.inner
                .$getter()
                .map(|values| values.into_iter().map(BinaryFunction::from_ffi).collect())
                .map_err(Error::from_ffi)
        }

        /// Replaces this pipeline-stage binary-function list.
        pub fn $setter(&self, values: &[BinaryFunction]) -> Result<(), Error> {
            let values: Vec<_> = values.iter().map(|value| value.inner.clone()).collect();
            self.inner.$setter(&values).map_err(Error::from_ffi)
        }
    };
}

impl RenderPipelineBinaryFunctionsDescriptor {
    facade_binary_function_list!(
        fragment_additional_binary_functions_vec,
        set_fragment_additional_binary_functions_slice
    );
    facade_binary_function_list!(
        mesh_additional_binary_functions_vec,
        set_mesh_additional_binary_functions_slice
    );
    facade_binary_function_list!(
        object_additional_binary_functions_vec,
        set_object_additional_binary_functions_slice
    );
    facade_binary_function_list!(
        tile_additional_binary_functions_vec,
        set_tile_additional_binary_functions_slice
    );
    facade_binary_function_list!(
        vertex_additional_binary_functions_vec,
        set_vertex_additional_binary_functions_slice
    );

    /// Restores every binary-function list to Metal defaults.
    pub fn reset_safe(&self) -> Result<(), Error> {
        self.inner.reset_safe().map_err(Error::from_ffi)
    }
}

impl RenderPipelineColorAttachmentDescriptor {
    /// Restores the attachment to Metal defaults.
    pub fn reset_safe(&self) -> Result<(), Error> {
        self.inner.reset_safe().map_err(Error::from_ffi)
    }
}

impl RenderPipelineColorAttachmentDescriptorArray {
    /// Returns the optional attachment in one of Metal's eight color slots.
    pub fn attachment(
        &self,
        index: usize,
    ) -> Result<Option<RenderPipelineColorAttachmentDescriptor>, Error> {
        self.inner
            .attachment(index)
            .map(|value| value.map(RenderPipelineColorAttachmentDescriptor::from_ffi))
            .map_err(Error::from_ffi)
    }

    /// Replaces the attachment in one of Metal's eight color slots.
    pub fn set_attachment(
        &self,
        index: usize,
        value: &RenderPipelineColorAttachmentDescriptor,
    ) -> Result<(), Error> {
        self.inner
            .set_attachment(index, &value.inner)
            .map_err(Error::from_ffi)
    }

    /// Restores the attachment array to Metal defaults.
    pub fn reset_safe(&self) -> Result<(), Error> {
        self.inner.reset_safe().map_err(Error::from_ffi)
    }
}

impl RenderPipelineDescriptor {
    /// Restores all render-pipeline properties to Metal defaults.
    pub fn reset_safe(&self) -> Result<(), Error> {
        self.inner.reset_safe().map_err(Error::from_ffi)
    }
}

macro_rules! facade_required_threads_descriptor {
    ($type:ty) => {
        impl $type {
            /// Returns the required threads-per-threadgroup size.
            pub fn required_threads_per_threadgroup_safe(&self) -> Result<Size, Error> {
                self.inner
                    .required_threads_per_threadgroup_safe()
                    .map_err(Error::from_ffi)
            }

            /// Sets or disables the required threads-per-threadgroup size.
            pub fn set_required_threads_per_threadgroup_safe(
                &self,
                size: Size,
            ) -> Result<(), Error> {
                self.inner
                    .set_required_threads_per_threadgroup_safe(size)
                    .map_err(Error::from_ffi)
            }

            /// Restores this pipeline descriptor to Metal defaults.
            pub fn reset_pipeline_descriptor(&self) -> Result<(), Error> {
                self.inner
                    .reset_pipeline_descriptor()
                    .map_err(Error::from_ffi)
            }
        }
    };
}

facade_required_threads_descriptor!(ComputePipelineDescriptor);
facade_required_threads_descriptor!(TileRenderPipelineDescriptor);

impl StitchedFunctionDescriptor {
    /// Returns the stitched function's component descriptors.
    pub fn function_descriptors_vec(&self) -> Result<Vec<FunctionDescriptor>, Error> {
        self.inner
            .function_descriptors_vec()
            .map(|values| {
                values
                    .into_iter()
                    .map(FunctionDescriptor::from_ffi)
                    .collect()
            })
            .map_err(Error::from_ffi)
    }

    /// Replaces the stitched function's component descriptors.
    pub fn set_function_descriptors_slice(
        &self,
        values: &[FunctionDescriptor],
    ) -> Result<(), Error> {
        let values: Vec<_> = values.iter().map(|value| value.inner.clone()).collect();
        self.inner
            .set_function_descriptors_slice(&values)
            .map_err(Error::from_ffi)
    }
}

impl RenderPassDescriptor {
    /// Returns up to `count` programmable sample positions.
    pub fn sample_positions_vec(&self, count: usize) -> Result<Vec<SamplePosition>, Error> {
        self.inner
            .sample_positions_vec(count)
            .map_err(Error::from_ffi)
    }

    /// Replaces programmable sample positions from a borrowed slice.
    pub fn set_sample_positions_slice(&self, values: &[SamplePosition]) -> Result<(), Error> {
        self.inner
            .set_sample_positions_slice(values)
            .map_err(Error::from_ffi)
    }
}

impl PipelineDataSetSerializer {
    /// Serializes captured binaries into an archive at a file path.
    pub fn serialize_archive_to_path(&self, path: impl AsRef<Path>) -> Result<(), Error> {
        self.inner
            .serialize_archive_to_path(path)
            .map_err(Error::from_ffi)
    }

    /// Serializes captured descriptors into owned pipeline-script bytes.
    pub fn serialize_pipelines_script(&self) -> Result<Vec<u8>, Error> {
        self.inner
            .serialize_pipelines_script()
            .map_err(Error::from_ffi)
    }
}

impl CommandAllocator {
    /// Returns the allocator's currently allocated byte count.
    pub fn allocated_size_safe(&self) -> Result<u64, Error> {
        self.inner.allocated_size_safe().map_err(Error::from_ffi)
    }

    /// Reclaims allocator storage no longer referenced by GPU work.
    pub fn reset_safe(&self) -> Result<(), Error> {
        self.inner.reset_safe().map_err(Error::from_ffi)
    }
}

impl CompilerTask {
    /// Blocks the calling thread until this compilation task finishes.
    pub fn wait_until_completed_safe(&self) -> Result<(), Error> {
        self.inner
            .wait_until_completed_safe()
            .map_err(Error::from_ffi)
    }
}