Skip to content

Commit

Permalink
Add MTLResourceID::from_raw and MTLResourceID::as_raw
Browse files Browse the repository at this point in the history
Fixes #660
  • Loading branch information
madsmtm committed Oct 3, 2024
1 parent ebbea02 commit 94f104f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/objc2/src/topics/about_generated/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Added `Eq` implementation for `NSValue` and subclasses.
* Added `CopyingHelper` and `MutableCopyingHelper`, which are used to specify
the types returned from `NSCopying` and `NSMutableCopying`.
* Added `MTLResourceID::from_raw` and `MTLResourceID::as_raw` to allow
querying the underlying data.

### Changed
* Allow using `MainThreadBound` without the `NSThread` feature flag.
Expand Down
2 changes: 2 additions & 0 deletions framework-crates/objc2-metal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ mod resource;
mod slice;
#[cfg(feature = "MTLTexture")]
mod texture;
#[cfg(feature = "MTLTypes")]
mod types;

#[cfg(feature = "MTLCounters")]
pub use self::counters::*;
Expand Down
30 changes: 30 additions & 0 deletions framework-crates/objc2-metal/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::MTLResourceID;

impl MTLResourceID {
/// Construct a `MTLResourceID` from an ID previously gotten via.
/// `to_raw`.
///
/// # Safety
///
/// The documentation for `MTLResourceID` says:
///
/// > A MTLResourceID represents a specific GPU resource, mutating this
/// > handle is undefined unless the mutation results in the value
/// > equalling an already existing handle of the same resource type.
///
/// So we've tentatively marked this method as `unsafe`, with the safety
/// requirement that the ID must be valid, i.e. have previously come from
/// [`to_raw`][Self::to_raw] or similar.
///
/// If you disagree with this assessment, feel free to open an issue!
pub const unsafe fn from_raw(id: u64) -> Self {
Self { _impl: id }
}

/// Get the underlying data of the ID.
///
/// May be useful for FFI purposes.
pub const fn to_raw(self) -> u64 {
self._impl
}
}

4 comments on commit 94f104f

@MarijnS95
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub doesn't allow commenting on arbitrary lines in commits, so I'm doing it like this:

    /// Construct a `MTLResourceID` from an ID previously gotten via.
    /// `to_raw`.

Shouldn't the dot on the first line be inside backticks, as if you're referencing to the .to_raw() method?

@madsmtm
Copy link
Owner Author

@madsmtm madsmtm commented on 94f104f Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a native speaker, I thought "via" was an abbreviation

@MarijnS95
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, then I see why there was a stray . there :)

@madsmtm
Copy link
Owner Author

@madsmtm madsmtm commented on 94f104f Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 67a4acd, and opened crate-ci/typos#1113, thanks!

Please sign in to comment.