Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rustの手続きマクロで黒魔術入門
Search
lemolatoon
March 11, 2023
Programming
1.4k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rustの手続きマクロで黒魔術入門
Rustの手続きマクロについて紹介します。そして、自作関数手続きマクロである direct_c_lang についても紹介します。
lemolatoon
March 11, 2023
More Decks by lemolatoon
See All by lemolatoon
スマホから Youtube Shortsを見られないようにする
lemolatoon
29
39k
お家miniPCを使ってミリしらでkubernetes clusterたててみる
lemolatoon
1
350
【Rust × C++】 夢の共演を試した話
lemolatoon
1
1.8k
no_stdでもasyncしたい
lemolatoon
0
280
Other Decks in Programming
See All in Programming
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
580
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
520
iOSDC2026登壇資料.pdf
riofujimon
0
200
Simple Storage Service(S3) is not simple
iwatsukayura
0
120
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
140
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
650
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
280
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
380
Are APIs Still Relevant in the AI Era?
soyuka
0
390
カツオ、ご期待ください
suneo3476
0
130
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
810
Featured
See All Featured
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
570
Documentation Writing (for coders)
carmenintech
77
5.6k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
500
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
340
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
How to Talk to Developers About Accessibility
jct
2
560
Scaling GitHub
holman
464
140k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Chasing Engaging Ingredients in Design
codingconduct
0
340
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