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
tonicの実装から学ぶトレイトとブランケット実装
Search
saw
January 23, 2025
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
tonicの実装から学ぶトレイトとブランケット実装
saw
January 23, 2025
More Decks by saw
See All by saw
本当は怖くない所有権の話
saw_poke
0
28
Featured
See All Featured
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
The Language of Interfaces
destraynor
162
27k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Bash Introduction
62gerente
615
220k
It's Worth the Effort
3n
188
29k
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
870
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Chasing Engaging Ingredients in Design
codingconduct
0
360
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
520
The SEO identity crisis: Don't let AI make you average
varn
0
590
Statistics for Hackers
jakevdp
799
230k
The Invisible Side of Design
smashingmag
301
52k
Transcript
前原 宗太朗 tonicの実装から学ぶトレイトとブランケット実装
前原 宗太朗 SOTARO MAEHARA • Rust初心者? • 実務ではTypeScriptとPHPを触っています • 趣味:
LoL
Rustのトレイトにおけるブランケット実装について知ると、Rust初級者から一歩 踏み出せるかも! Rustの構文にも慣れてきて公式サイトや書籍を見ながら実装ができる! ⇨ そのライブラリがどういう理屈で動いているのかが理解できる tonic(gRPCフレームワーク)を例に見ていくよ! 今日のポイント
共通の振る舞いを定義するための機能 役割としてはインターフェースに近いかも トレイトとは pub trait Summary { fn summarize(&self) ->
String; } 共通の振る舞いとしてsummarize関数を持つSummaryというトレイトを定義
型に対して、トレイトを実装することができる トレイトとは pub struct Post { pub username: String, pub
content: String, } impl Summary for Post { fn summarize(&self) -> String { format!("{}: {}", self.username, self.content) } }
トレイトを実装するとインスタンスに対してメソッドを呼び出すことができる トレイトとは let post = Post { username: String::from("horse_ebooks"), content:
String::from("sample contents”), }; println!("1 new post: {}",post.summarize());
1.デフォルト実装を持たせることができる インターフェースとの違い pub trait Summary { fn summarize(&self) -> String
{ String::from("(Read more...)") } } 2. 既存の型に対しても、継承関係なしに実装を加えることができる
message Point {} message Feature {} service Client { rpc
GetFeature(Point) returns (Feature); } tonic clientにおけるトレイト pub struct Point {} pub struct Client {} impl Client { fn get_feature(&self, r: impl tonic::IntoRequest<Point>) {} } tonicでは以下のようなprotoから、右のコードが自動生成されます get_featureはIntoRequest<Point>を引数に要求しています
ジェネリクス型パラメータ T をtonic::Requestでラップしたものを返す into_request メソッドを実装している型であれば、どのような型でもOK! IntoRequestの詳細 pub trait IntoRequest<T>: sealed::Sealed
{ fn into_request(self) -> Request<T>; } pub struct Point {} pub struct Client {} impl Client { fn get_feature(&self, r: impl tonic::IntoRequest<Point>) {} }
次のように呼び出すことができます 自動生成されたClientコードの使い方 use tonic::Request; client.get_feature(Point {}); client.get_feature(Request::new(Point {})); 2つの例がありますが、どちらも有効な呼び出しです PointとRequest<Point>の両方がIntoRequest<Point>を満たしているため
ここでの疑問は、どのようにしてIntoRequestを満たしているのか
トレイトの制約を満たすあらゆる型に対して、トレイトを実装することができる ブランケット実装 impl<T> IntoRequest<T> for Request<T> { fn into_request(self) ->
Request<T> { self } } impl<T> IntoRequest<T> for T { fn into_request(self) -> Request<Self> { Request::new(self) } } 全ての型TとRequest<T>に対してIntoRequestを実装している そのため、PointとRequest<Point>をget_featureに渡すことができる
Axumのハンドラーは関数を定義するといい感じにRouterに渡すことができる おまけ pub async fn hoge_handler( Json(request): Json<HogeRequest>, ) ->
Result<Json<HogeResponse>, Error> { // ... } pub async fn fuga_handler( Path(fuga_id): Json<string>, ) -> Result<Json<FugaResponse>, Error> { // ... } ブランケット実装(とマクロ)によって、関数(FnOnce)に対してハンドラのトレイトを 実装しています
まとめ Rustのトレイトにおけるブランケット実装について知ると、Rust初級者から一歩 踏み出せるかも! Rustの構文にも慣れてきて公式サイトや書籍を見ながら実装ができる! ⇨ そのライブラリがどういう理屈で動いているのかが理解できる tonic clientでは引数にIntoRequestを要求することがあるが、ブランケット実装 によってトレイトを満たす実装がされているよ!