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
Linda_pp
June 27, 2017
Technology
3
1.5k
Rust で遅延評価を 実装してみる
RustのLT会! Rust入門者の集い #3
Linda_pp
June 27, 2017
Tweet
Share
More Decks by Linda_pp
See All by Linda_pp
actionlint の Linter 設計
rhysd
6
3.6k
ripgrep をライブラリとして使う
rhysd
0
410
port-monolith-to-wasm-for-chrome-extension
rhysd
0
450
Fuzzing Rust Text Editor
rhysd
1
2.9k
Vim compiled to WebAssembly
rhysd
5
2.1k
about-neovim-0.4.0-floating-window
rhysd
3
2.2k
reply.vim
rhysd
0
1.2k
Vim ported to WebAssembly (VimConf 2018)
rhysd
4
3.3k
go-selfupdate-github で ツールを自己アップデートする
rhysd
5
4.3k
Other Decks in Technology
See All in Technology
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
Amazon VPC Lattice 最新アップデート紹介 - PrivateLink も似たようなアップデートあったけど違いとは
bigmuramura
0
190
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
Jetpack Composeで始めるServer Cache State
ogaclejapan
2
160
How to be an AWS Community Builder | 君もAWS Community Builderになろう!〜2024 冬 CB募集直前対策編?!〜
coosuke
PRO
2
2.8k
組織に自動テストを書く文化を根付かせる戦略(2024冬版) / Building Automated Test Culture 2024 Winter Edition
twada
PRO
12
3.4k
Wantedly での Datadog 活用事例
bgpat
1
410
Microsoft Azure全冠になってみた ~アレを使い倒した者が試験を制す!?~/Obtained all Microsoft Azure certifications Those who use "that" to the full will win the exam! ?
yuj1osm
1
110
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
280
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
180
5分でわかるDuckDB
chanyou0311
10
3.2k
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
What's in a price? How to price your products and services
michaelherold
243
12k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Done Done
chrislema
181
16k
KATA
mclloyd
29
14k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Making Projects Easy
brettharned
116
5.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Transcript
Rust ͰԆධՁΛ ࣮ͯ͠ΈΔ RustͷLTձʂ Rustೖऀͷू͍ #3 @Linda_pp @rhysd
͋Β͢͡ • ७ਮؔܕσʔλߏΛಡΈͳ͕ Β Rust Ͱ࣮͍ͯͨ͋͠Δɼ ԆධՁΛ͏σʔλߏ͕ग़ݱ • Rust ԆධՁ͡Όͳ͍…
• ࣮ͨ͠ https://goo.gl/qMGuZQ
ԆධՁͱ ࣜΛ͙͢ʹධՁͤͣɼඞཁʹͳͬͨ࣌ʹධ Ձ͢Δ
Ԇ͠ͳ͍ධՁ 1 // 1000ݸͷૉΛͭ͘ΔʢΊͬͪΌॏ͍ॲཧʣ 2 let n = primes1000(); 3
4 // ... 5 6 // 100൪දࣔ 7 println!("{}", vec[99]); 8 9 // 1000൪දࣔ 10 println!("{}", vec[999]); ιʔεɿ https://goo.gl/qMGuZQ
ԆධՁ 1 // lazily! Ͱғ·ΕͨࣜԆධՁ͞ΕΔ 2 // ͜ͷ࣌Ͱ vec ܭࢉ͞Ε͍ͯͳ͍
3 let vec = lazily!{ 4 prime1000th() 5 }; 6 7 // vec Λ༻͢ΔλΠϛϯάͰॳΊͯ vec ΛධՁ 8 println!("{}", vec[99]); 9 10 // ҰධՁͨ݁͠ՌΩϟογϡ͞Ε͍ͯ·Θ͞ΕΔ 11 println!("{}", vec[999]); ιʔεɿ https://goo.gl/qMGuZQ
σʔλͷ࣋ͪํ 1 // αϯΫɿܭࢉΛද͢σʔλߏ 2 enum Thunk<'a, T: 'a> {
3 // ·ͩܭࢉ͞Εͯͳ͍ঢ়ଶΛΫϩʔδϟͰ࣋ͭ 4 NotYet(Box<Fn() -> T + 'a>), 5 // ܭࢉࡁΈ 6 Memo(T), 7 } 8 9 // Ԇ͞ΕͨࣜΛද͢ߏମ Delayed 10 pub struct Delayed<'a, T: 'a> { 11 thunk: RefCell<Box<Thunk<'a, T>>> 12 } ιʔεɿ https://goo.gl/qMGuZQ
σʔλͷ࣋ͪํ 1 impl<'a, T: 'a> Delayed<'a, T> { 2 //
৽͍͠ԆධՁࣜΛੜ 3 // ΫϩʔδϟΛड͚औͬͯ RefCell ʹಥͬࠐΉ 4 pub fn new<F>(f: F) -> Self where F: Fn() -> T + 'a { 5 Delayed { thunk: RefCell::new( 6 Box::new( 7 NotYet(Box::new(f)) 8 ) 9 ) } 10 } 11 } 12 13 // ϚΫϩͰ move Ωϟϓνϟ͢ΔΫϩʔδϟʹల։͢Δ 14 // lazily!{42} -> Delayed::new(move || { 42 }) 15 #[macro_export] 16 macro_rules! lazily { 17 ($($b:tt)+) => { 18 self::Delayed::new(move || { $($b)+ }) 19 } 20 } ιʔεɿ https://goo.gl/qMGuZQ
force: Ԇ͞ΕͨࣜΛධՁ 1 impl<'a, T: 'a> Delayed<'a, T> { 2
pub fn force(&self) { 3 // ͜͜Ͱ mut ʹ͠ͳ͍ͱαϯΫ͕ߋ৽Ͱ͖ͳ͍ͷͰɼ 4 // RefCell ͰแΉ 5 let mut thunk = &mut *self.thunk.borrow_mut(); 6 let val = match **thunk { 7 // ະධՁͳΒอ͍࣋ͯͨ͠ΫϩʔδϟΛ࣮ߦ͠ɼ 8 // ܭࢉΛ࣮ߦ͢Δɽ 9 NotYet(ref invoke) => { 10 Box::new(Memo(invoke())) 11 }, 12 // ͢ͰʹܭࢉࡁΈͳΒԿ͠ͳ͍ 13 Memo(_) => return, 14 }; 15 // ܭࢉͨ͠Λ͓࣋ͬͯ͘ 16 *thunk = val; 17 } 18 } ιʔεɿ https://goo.gl/qMGuZQ
ԆධՁͷλΠϛϯά Rc<T> ͕͍ͬͯΔΑ͏ʹɼDerefτϨΠτ Λͬͯɼϝιουݺͼग़͠ͷλΠϛϯάͰ ࣗಈͰத͕ධՁ͞ΕΔΑ͏ʹ͢Δ 1 let s = lazily!("foo".to_string())
2 3 // s ͕ࣗಈͰධՁ͞ΕΔ 4 s.as_str(); 5 6 // * ԋࢉࢠͰ໌ࣔతʹධՁͰ͖Δ 7 *s; ιʔεɿ https://goo.gl/qMGuZQ
Deref Ͱࢀর࣌ʹࣗಈධՁ 1 impl<'a, T: 'a> Deref for Delayed<'a, T>
{ 2 // T ͕ධՁ݁Ռͷͷܕ 3 type Target = T; 4 fn deref(&self) -> &T { 5 self.force(); 6 // RefCell ͷதΛҰ࣌తʹऔΓग़͢ 7 let thunk = unsafe { 8 self.thunk.as_ptr().as_ref().unwrap() 9 }; 10 match **thunk { 11 // αϯΫͷதͷࢀরΛฦ͢ 12 Memo(ref v) => v, 13 _ => unreachable!(), 14 } 15 } 16 } ιʔεɿ https://goo.gl/qMGuZQ
ͭͬͨ͘ԆධՁͰԆϦετ Λͭͬͨ͘ײ • ΄΅ॻ੶ͷઆ໌௨Γʹίʔυ͕ॻ͚ͯྑ͍ײ͡ • Deref ༧֎ͷλΠϛϯάͰܭࢉ͕࣮ߦ͞Εͯ͠· ͏͜ͱ͕͋ͬͨɽ • Clone
Λ࣮͢ΔͨΊʹ݁ہ Box Λ Rc ʹ͢Δඞཁ͕ ͋ͬͨ • ϜʔϒΩϟϓνϟͰण໋͕བྷΉͷͰɼlazily! Ͱੜ͠ ͨͷѻ͍͕Ϝζ͍ ιʔεɿ https://goo.gl/qMGuZQ