Skip to main content

mtl_gpu/
allocation.rs

1//! Allocation protocol.
2//!
3//! Corresponds to `Metal/MTLAllocation.hpp`.
4//!
5//! This module defines the Allocation trait which corresponds to the
6//! `MTL::Allocation` protocol in metal-cpp. Resource types like Buffer,
7//! Texture, and Heap already have `allocated_size()` as an inherent method,
8//! so this trait serves primarily for documentation and generic programming.
9
10use mtl_foundation::UInteger;
11
12/// Protocol for types that track their allocated memory size.
13///
14/// C++ equivalent: `MTL::Allocation`
15///
16/// This trait corresponds to the Objective-C protocol that all Metal resource
17/// types conform to. In Rust, concrete types like [`Buffer`](crate::Buffer),
18/// [`Texture`](crate::Texture), and [`Heap`](crate::Heap) implement this
19/// method directly.
20///
21/// # Example
22///
23/// ```ignore
24/// // All resource types have allocated_size() directly
25/// let buffer = device.new_buffer(1024, ResourceOptions::default()).unwrap();
26/// println!("Allocated: {} bytes", buffer.allocated_size());
27/// ```
28pub trait Allocation {
29    /// Get the allocated size of this resource in bytes.
30    ///
31    /// This may be larger than the requested size due to alignment requirements.
32    ///
33    /// C++ equivalent: `NS::UInteger allocatedSize() const`
34    fn allocated_size(&self) -> UInteger;
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_allocation_trait() {
43        // Verify the trait is object-safe
44        fn _check_object_safety(_: &dyn Allocation) {}
45    }
46}