Share
Embed
Transcript
61 slides
-
WebAssembly • Web্(≒ϒϥβ্)Ͱಈ͘όΠφϦܗࣜ • ߴɺߴޮɺϙʔλϒϧ • js 㱻 wasm ؒͰΓͱΓ͕Ͱ͖Δ
• js ͷஔ͖͑Λࢦ͍ͯ͠ΔΘ͚Ͱͳ͍ 4 -
WebAssemblyςΩετܗࣜ (module (func $i (import "imports" "imported_func") (param i32)) (func
(export "exported_func") i32.const 42 call $i ) ) 6 -
js͔Β wasm Λ͏ • import puyo from 'path/to/wasm/module'; • ͱͰ͖ͳ͍ʂ
• fetch→instanciate→function call 8 -
js͔Β wasm Λ͏ྫ const importObject = { imports: { imported_func:
(arg) => console.log(arg) } }; WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then( (obj) => obj.instance.exports.exported_func() ); 9 -
WASI • "Web"Assembly Web ͚ͩͷͷʹ͠ͳͯ͘ྑ͍ͷͰʁ • →ଞͷڥͰಈ͔ͦ͏ • OSͱͷΓͱΓ=γεςϜίʔϧ͕ඞཁ
• OSநԽͱͯ͠ͷ༷ఆٛ • wasi-core, wasi-libc ͳͲϞδϡʔϧϕʔεͰ֦ுத • WASI ରԠ wasm ͳΒϒϥβ֎Ͱ࣮ߦՄೳɻݱࡏCͱRustͷΈରԠ 10 -
Ownership { // aɺ͜͜Ͱ༗ޮͰͳ͍ɻ·ͩએݴ͞Ε͍ͯͳ͍ let a = 5; // aɺ͔͜͜Β༗ޮʹͳΔ
// aͰ࡞ۀΛ͢Δ } // ͜ͷείʔϓऴΘΓɻ͏a༗ޮͰͳ͍ 26 -
Ownership(ೖ) { let x = 5; let y = x;
println!("x = {}, y = {}", x, y); } 27 -
Ownership(ೖ) { let s1 = String::from("hello"); let s2 = s1;
println!("{}, world!", s1); } 28 -
Ownership(ೖ) • ઌͷίʔυಈ͔ͳ͍ʂ • จࣈྻͷϙΠϯλΛ࣋ͭ • ೖ͢ΔͱϙΠϯλ͕ίϐʔ͞ΕΔ(shallow copy) • Rust
Ͱɺs1 Λ s2 ʹೖͨ࣌͠Ͱจࣈྻͷॴ༗ऀ͕ s2 ʹҠΔ ˠmove 29 -
Ownership(ೖ) • ͋Εʁͷͱ͖ಈ͍͚ͨͲɾɾɾʁ • ͷ߹ɺshallow copy ͱ deep copy ͷ۠ผ͕ແ͍(ཁΒͳ͍)
• ۠ผ͕ཁΒͳ͍ܕͷྫ: u32, bool, char ͳͲ 30 -
Ownership(ؔ) { let s = String::from("hello"); // s͕είʔϓʹೖΔ fn_hoge(s); //
sͷ͕ؔʹϜʔϒ͞Ε... // ... ͜͜Ͱ͏༗ޮͰͳ͍ let x = 5; // x͕είʔϓʹೖΔ fn_fuga(x); // xؔʹϜʔϒ͞ΕΔ͕ɺ // i32CopyͳͷͰɺ͜ͷޙʹxΛͬͯେৎ } // ͜͜Ͱx͕είʔϓΛൈ͚ɺsείʔϓΛൈ͚Δɻͨͩ͠ɺsͷϜʔϒ͞Ε͍ͯΔͷͰɺԿಛผͳ͜ͱى͜Βͳ͍ɻ 32 -
Ownership(ؔ) fn main() { let s1 = gives_ownership(); // gives_ownershipɺΓΛs1ʹϜʔϒ͢Δ
let s2 = String::from("hello"); // s2͕είʔϓʹೖΔ let s3 = takes_and_gives_back(s2); // s2takes_and_gives_backʹϜʔϒ͞ΕΓs3ʹϜʔϒ͞ΕΔ } // ͜͜Ͱɺs3είʔϓΛൈ͚υϩοϓ͞ΕΔɻs2είʔϓΛൈ͚Δ͕ɺϜʔϒ͞Ε͍ͯΔͷͰԿى͖ͳ͍ɻs1είʔϓΛൈ͚υϩοϓ͞ΕΔɻ fn gives_ownership() -> String { let some_string = String::from("hello"); // some_string͕είʔϓʹೖΔ some_string // some_string͕ฦ͞Εɺݺͼग़͠ݩؔʹϜʔϒ͞ΕΔ } fn takes_and_gives_back(a_string: String) -> String { // a_string͕είʔϓʹೖΔɻ a_string // a_string͕ฦ͞Εɺݺͼग़͠ݩؔʹϜʔϒ͞ΕΔ } 33 -
Ownership(ࢀর) fn main() { let s1 = String::from("hello"); let len
= calculate_length(&s1); } fn calculate_length(s: &String) -> usize { // sStringͷࢀর s.len() } // ͜͜Ͱɺsείʔϓ֎ʹͳΔ͕ɺࢀর͍ͯ͠Δͷͷॴ༗ݖΛ͍࣋ͬͯΔΘ͚Ͱͳ͍ͷͰԿى͜Βͳ͍ 35 -
Ownership(ࢀর) fn main() { let s = String::from("hello"); change(&s); }
// आ༻ͨ͠Կ͔Λมߋ͍ͨ͠ʂ fn change(some_string: &String) { some_string.push_str(", world"); } 36 -
Ownership(ࢀর) fn main() { // มσϑΥϧτͰෆมͳͨΊɺmutΛ༩ let mut s =
String::from("hello"); change(&mut s); } fn change(some_string: &mut String) { some_string.push_str(", world"); } 37 -
Pattern Matching enum Coin { Penny, Nickel, Dime, Quarter, }
fn value_in_cents(coin: Coin) -> u32 { match coin { Coin::Penny => 1, Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, } } 40 -
Pattern Matching(Ψʔυ) match x { 0 => println!("zero"), 1 =>
println!("one"), n => if is_prime(n) { println!("prime number {}", n); } else { println!("composite number {}", n); } } match x { 0 => println!("zero"), 1 => println!("one"), n if is_prime(n) => println!("prime number {}", n), n => println!("composite number {}", n) } 42 -
Pattern Matching(෦ଋറ) fn plus_one(x: Option<i32>) -> Option<i32> { match x
{ None => None, Some(i) => Some(i + 1), } } let five = Some(5); let six = plus_one(five); let none = plus_one(None); 46 -
Pattern Matching(ม) struct Point { x: i32, y: i32, }
let ((hoge, fuga), Point {a, b}) = ((3, 10), Point { x: 3, y: -10 }); 48 -
Trait(࣮) struct IceCream { unit_price: f64, flavor: String, } struct
EnglishClass { hourly_price: f64, hour: f64, difficulty_level: String, } impl Purchasable for IceCream { fn get_subtotal_price(&self) -> f64 { self.unit_price } } impl Purchasable for EnglishClass { fn get_subtotal_price(&self) -> f64 { &self.hourly_price * &self.hour } } 53 -
Trait(σϑΥϧτ࣮) const DEFAULT_CONSUMPTION_TAX_RATE: f64 = 10.0; trait Purchasable { fn
get_subtotal_price(&self) -> f64; // σϑΥϧτ࣮͋Γ fn get_tax_included_price(&self) -> f64 { let subtotal = &self.get_subtotal_price(); subtotal / DEFAULT_CONSUMPTION_TAX_RATE + subtotal } } 55 -
Trait(σϑΥϧτ࣮) fn main() { let vanilla_ice = IceCream { unit_price:
400.0, flavor: String::from("vanilla"), }; let some_class = EnglishClass { hourly_price: 2000.0, hour: 3.0, difficulty_level: String::from("hard"), }; println!("{}", vanilla_ice.get_tax_included_price()); // 440 println!("{}", some_class.get_tax_included_price()); // 6600 } 56 -
Trait(ؔ) fn get_tax_amount(arg: &impl Purchasable) -> f64 { let tax_included_price
= arg.get_tax_included_price(); let subtotal = arg.get_subtotal_price(); tax_included_price - subtotal } // δΣωϦοΫܕΛͬͯॻ͘͜ͱͰ͖Δ fn get_tax_amount<T: Purchasable>(arg: &T) -> f64 { let tax_included_price = arg.get_tax_included_price(); let subtotal = arg.get_subtotal_price(); tax_included_price - subtotal } 58 -
Trait(ؔ) fn get_vanilla_ice() -> impl Purchasable { IceCream { unit_price:
400.0, flavor: String::from("vanilla"), } } fn main() { let vanilla_ice = get_vanilla_ice(); // vanilla_ice ͷܕ impl Purchasable } 59 -
ࢀߟ • https://webassembly.org/ • https://developer.mozilla.org/ja/docs/WebAssembly • https://doc.rust-jp.rs/book-ja/ • https://numb86-tech.hatenablog.com/entry/2021/09/14/210955 •
https://gist.github.com/omasanori/7858569 • https://zenn.dev/mebiusbox/books/22d4c1ed9b0003 61