项目地址:github.com/gfx-rs/meta…

Rust 表达 Objective-C new操作

pub enum MTLHeapDescriptor {}foreign_obj_type! {type CType = MTLHeapDescriptor;pub struct HeapDescriptor;pub struct HeapDescriptorRef;
}impl HeapDescriptor {pub fn new() -> Self {unsafe {let class = class!(MTLHeapDescriptor);msg_send![class, new]}}
}impl HeapDescriptorRef {pub fn cpu_cache_mode(&self) -> MTLCPUCacheMode {unsafe { msg_send![self, cpuCacheMode] }}pub fn set_cpu_cache_mode(&self, mode: MTLCPUCacheMode) {unsafe { msg_send![self, setCpuCacheMode: mode] }}pub fn storage_mode(&self) -> MTLStorageMode {unsafe { msg_send![self, storageMode] }}pub fn set_storage_mode(&self, mode: MTLStorageMode) {unsafe { msg_send![self, setStorageMode: mode] }}pub fn size(&self) -> NSUInteger {unsafe { msg_send![self, size] }}pub fn set_size(&self, size: NSUInteger) {unsafe {msg_send![self, setSize: size];}}
}
复制代码

Rust 表达 Objective-C 继承关系

父类CAMetalDrawable继承自NSObject,用Rust表达如下。

pub enum MTLDrawable {}foreign_obj_type! {type CType = MTLDrawable;pub struct Drawable;pub struct DrawableRef;
}impl DrawableRef {pub fn present(&self) {unsafe {msg_send![self, present]}}
}
复制代码

子类CAMetalDrawable用Rust表达如下。

pub enum CAMetalDrawable {}foreign_obj_type! {type CType = CAMetalDrawable;pub struct CoreAnimationDrawable;pub struct CoreAnimationDrawableRef;type ParentType = DrawableRef;
}impl CoreAnimationDrawableRef {pub fn texture(&self) -> &TextureRef {unsafe { msg_send![self, texture] }}
}
复制代码

工具宏

macro_rules! foreign_obj_type {{type CType = $raw_ident:ident;pub struct $owned_ident:ident;pub struct $ref_ident:ident;type ParentType = $parent_ref:ident;} => {foreign_obj_type! {type CType = $raw_ident;pub struct $owned_ident;pub struct $ref_ident;}impl ::std::ops::Deref for $ref_ident {type Target = $parent_ref;fn deref(&self) -> &$parent_ref {unsafe { &*(self as *const $ref_ident as *const $parent_ref)  }}}};{type CType = $raw_ident:ident;pub struct $owned_ident:ident;pub struct $ref_ident:ident;} => {foreign_type! {type CType = $raw_ident;fn drop = ::obj_drop;fn clone = ::obj_clone;pub struct $owned_ident;pub struct $ref_ident;}unsafe impl ::objc::Message for $raw_ident {}unsafe impl ::objc::Message for $ref_ident {}impl ::std::fmt::Debug for $ref_ident {fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {unsafe {use ::objc_foundation::INSString;// TODO: might leak, not 100% sure...let string: &::objc_foundation::NSString = msg_send![self, debugDescription];write!(f, "{}", string.as_str())}}}impl ::std::fmt::Debug for $owned_ident {fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {::std::ops::Deref::deref(self).fmt(f)}}};
}
复制代码

辅助项目

上述操作依赖如下项目:

  • foreign-types
  • rust-objc

Rust 与 Objective-C 互操作相关推荐

  1. Rust 生命周期太难学、最想实现与 C++ 互操作,Rust 2020 调查报告发布!

    编译 | 苏宓 头图 | CSDN 下载自东方 IC 出品 | CSDN(ID:CSDNnews) 众多语言中,Rust 作为一款小众的现代系统编程语言,近几年间,接连受到了各大企业的青睐. 正如不久 ...

  2. Rust 2020 调查报告出炉,95%的开发者吐槽Rust难学

    来源 | CSDN(ID:CSDNnews) 编译 | 苏宓 头图 | CSDN 下载自东方 IC 众多语言中,Rust 作为一款小众的现代系统编程语言,近几年间,接连受到了各大企业的青睐. 正如不久 ...

  3. 【Rust日报】2022-09-11 Shuttle 创建和部署带有ShuttleSerenity的 Discord 机器人!

    Shuttle v0.5.1 已发布 ━ 创建和部署一个带有Shuttle & Serenity 的 Discord 机器人! 对于任何想要让 Discord 机器人启动并运行(使用 Rust ...

  4. rust木炭有用吗_Rust 2020 调查报告出炉,95%的开发者吐槽Rust难学

    同时,今年 3 月,苹果也表示对将 C 代码移植到 Rust 感兴趣,希望投身到使用 Rust 编写代码的潮流中.此前通过苹果的一则招聘公告可以看出,其想要在基于 Linux 的服务器端平台上将 C ...

  5. Kotlin威胁、Python逆袭,2018年程序员需要升级哪些技能?(附报告下载)

    来源:CSDN 本文共6700字,建议阅读10分钟. 本文从App开发.Web开发.安全和系统管理.数据科学四大方面讲述了2018年开发者运用的工具和获得高薪的技能. [ 导读 ]近日,著名的技术书籍 ...

  6. delphi windows编程_2020年值得关注的新编程V语言Vlang,对标Golang、Rust、Swift

    编程语言的世界总是能搞出新花样,虽然基本原理都是万变不离其宗,但是对我们个人而言,选择好一个语言的确可以事半功倍,何乐而不为呢? 要说目前2020年比较新奇的语言,就得说说V语言Vlang了,MIT开 ...

  7. rust为什么显示不了国服_AWS偏爱Rust,已将Rust编译器团队负责人收入囊中

    机器之心报道 作者:张倩.杜伟 近日,AWS 透露,该公司已经聘用了 Rust 编译器团队负责人之一 Felix Klock.该消息出自 AWS 开源团队于上周二发布的一篇文章<Why AWS ...

  8. Programming Rust Fast, Safe Systems Development(译) 表达式(第六章 完)

    LISP programmers know the value of everything, but the cost of nothing. -Alan Perlis, epigram #55 在本 ...

  9. rust实现一个mysql驱动_使用Rust编写用户态驱动程序

    概览 在云计算技术的发展史上,如何提高单个服务器的并发度,一直是热门的研究课题.在20年前,就有著名的"C10K"问题,即如何利用单个服务器每秒应对10K个客户端的同时访问.这么多 ...

最新文章

  1. Xcode7中创建静态库
  2. 转Python 参数知识(变量前加星号的意义)
  3. php 跨域名存储cookie,实现跨域名Cookie
  4. 丹鸟快递承诺达到不了怎么办_谈谈2019年快递行业的竞争
  5. [书籍分享]0-003.你的灯亮着吗:发现问题的真正所在
  6. HashMap中最多只允许一条记录的键为Null,允许多条记录的值为Null
  7. c语言mergesort 参数,归并排序C语言兑现MergeSort
  8. php默认语法,php语法基本规则
  9. Unity面试题精选(3)
  10. 棋牌app开发需要多少钱
  11. 要读的书---培根说:历史使人明智,诗词使人巧慧,算学使人精密,哲理使人深刻,伦理学人庄重,逻辑修辞使人善辩。...
  12. 2021深圳观澜中学高考成绩查询入口,2019深圳龙华中学、观澜中学、龙华高级中学录取分数线及高考成绩喜报...
  13. keep-alive的用法和作用
  14. DRM dumb,prime介绍
  15. 手机号码界面输入数字查看手机信息
  16. 关于HTML在线编辑文本的编码与解码
  17. 冒泡排序图解-Java实现
  18. vue中的created和mounted函数不生效(created和mounted不管用)?
  19. 软件质量控制问题与质量控制技术
  20. 这才是没有文化的表现

热门文章

  1. 了解 sourceMap 配置
  2. 获得对摄像头的访问权
  3. 看看webpack打包优化
  4. php进程名,DOS根据进程名或PID删除进程命令
  5. android退出图标按钮,android-setCloseButtonIcon(位图可绘制)不适用于...
  6. 2014计算机中山大学新华学院分数线,中山大学新华学院历年分数线 2021中山大学新华学院录取分数线...
  7. 20210627:力扣第247周周赛(上)
  8. 20210602:力扣第243周周赛(下)
  9. 反应机理_介绍一种化学反应机理合成环戊酮
  10. denied git permission_Git使用之Permission Denied问题解决[2153-Noblog]