Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rustの手続きマクロで黒魔術入門
Search
lemolatoon
March 11, 2023
Programming
2
1.3k
Rustの手続きマクロで黒魔術入門
Rustの手続きマクロについて紹介します。そして、自作関数手続きマクロである direct_c_lang についても紹介します。
lemolatoon
March 11, 2023
Tweet
Share
More Decks by lemolatoon
See All by lemolatoon
スマホから Youtube Shortsを見られないようにする
lemolatoon
29
38k
お家miniPCを使ってミリしらでkubernetes clusterたててみる
lemolatoon
1
240
【Rust × C++】 夢の共演を試した話
lemolatoon
1
1.7k
no_stdでもasyncしたい
lemolatoon
0
260
Other Decks in Programming
See All in Programming
Codexに役割を持たせる 他のAIエージェントと組み合わせる実務Tips
o8n
3
1.1k
AWS Infrastructure as Code の新機能 2025 総まとめ 〜SA 4人による怒涛のデモ祭り〜
konokenj
10
3.3k
Swift ConcurrencyでよりSwiftyに
yuukiw00w
0
250
Geminiの機能を調べ尽くしてみた
naruyoshimi
0
200
コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions
handlename
0
170
15年目のiOSアプリを1から作り直す技術
teakun
1
610
Windows on Ryzen and I
seosoft
0
210
AI活用のコスパを最大化する方法
ochtum
0
120
エージェント開発初心者の僕がエージェントを作った話と今後やりたいこと
thasu0123
0
230
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
1
130
CopilotKit + AG-UIを学ぶ
nearme_tech
PRO
2
140
Docコメントで始める簡単ガードレール
keisukeikeda
1
100
Featured
See All Featured
Docker and Python
trallard
47
3.8k
BBQ
matthewcrist
89
10k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
660
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.2k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
190
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
61
52k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Mobile First: as difficult as doing things right
swwweet
225
10k
Embracing the Ebb and Flow
colly
88
5k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
140
Transcript
Rustの手続きマクロで黒魔術入門 lemolatoon
自己紹介 慶應義塾大学 理工学部1年 セキュリティ・キャンプ2022 Cコンパイラゼミ修了生 Twitter (@lemolatoon1)
(このスライドは上記アカウントでツイートしています) 2
“Rustの手続きマクロ”って? Rustを書くときによく見る手続きマクロの例 1) deriveマクロ 2) attributeマクロ 3 ソースコードを受け取って、ソースコードを返すようなプログラミング言語Rustの特殊な関数
3) 関数マクロ
自由度の高い関数手続きマクロ 4 * カッコの対応などは入力時点で正しい必要がある 関数手続きマクロは正しいRustのコードを出力すれば、 どんな入力*でも受け付ける。
自作関数手続きマクロ direct_c_lang の紹介 5 https://github.com/lemolatoon/direct_c_lang_rs.git C言語のコードを受け取り、Rustから呼べるインターフェースを提供する。 (下記は実際に使っている例 ) Rust側からマクロ内で定義されたC言語
で書かれた関数を呼び出している。 (4~10行) C言語のソースコードをマクロに渡し ている。 (12~25行)
自作関数手続きマクロ direct_c_lang の紹介(マクロの展開) 6 ◼マクロの入力を global_asmマクロ+C言語側の関数 の宣言へ変換 ◼マクロ内部で自作Cコンパイラを使っている ◼マクロ展開前 ◼マクロ展開後
実際の手続きマクロの中身 7 * https://github.com/lemolatoon/direct_c_lang_rs.git マクロの変換前から、マクロの変換後の処理自体をRustでかける。 中略(Cの関数をRustの宣言に変換) マクロの入力として与えられ たC言語のソースコードを アセンブリに変換
マクロの出力(展開先)を関 数の戻り値として返す
まとめ Rustの手続きマクロは自由度が高い上に、マクロ処理自体をRustでかけるため、 C/C++のような複雑性が生じにくい 自由度を生かして自分の独自のマクロを作ると楽しい 「書けばいいことは明確になったけど、これ全部手で書かなきゃいけないのか ……」といったときに便利 8
ありがとうございました 9