スマートポインターの独自実装(@Rust)

Box<T>のようなスマートポインターの独自実装がRustドキュメントの中に出てきます。Box<T>は一番基本的なスマートポインターで他にもいくつか存在しますが、

https://doc.rust-jp.rs/book-ja/ch15-02-deref.html

もともとスマートポインターはRust固有ではなくC++でもありますが、定義は『通常のポインタのように振る舞うだけでなく、追加のメタデータと能力があるデータ構造』とありますが、追加のメタデータと能力とは以下のサンプルコードで示される参照外しは一例かと思います。もともとC++におけるスマートポインターも目的とするところはほぼ同じ。

以下のコードはリンクからのコードですが、独自のMyBox(Box相当)でBoxのメソッドの一つであるnew()を実装すれば、引数の値をヒープエリアに割り当ててくれます。ただし、DerefをMyBoxメソッドに対して実装しないとせっかくMyBoxで割り当てたメモリエリアの参照ができないので、以下のコードはコンパイルできません。

hello関数と呼出の例は、もしMyBoxにDerefが実装されていないと呼び出しが煩雑になるよという例を示すためで、もしDeref実装がないと以下のような読みづらいコードを書かないといけないようです。

hello(&m); –> hello(&(*m)[..]);

これは、

https://qiita.com/moyamoyac/items/5aea471d6676625dcd62

で記述されているように、以下の三段階の変換が組み込まれたコードなのでぱっと見分かりづらい。

mMyBox<String>
*mString
&(*m)[..]&str

 

use std::ops::Deref;

struct MyBox<T>(T);

impl MyBox<T> {      // original smart pointer
    fn new(x: T) -> MyBox {
        MyBox(x)
    }
}

impl Deref for MyBox {    // deref coercion
    type Target = T;    // to define return value type T for the Deref "Target"
                        // in the DeRef trait : fn deref(&self) -> &Self::Target;

    fn deref(&self) -> &T {
        &self.0
    }
}

fn hello(name: &str) {          // "&" is needed to get a known size of the argument at compile-time
    println!("Hello, {}!", name);
}

fn main() {
    let x = 5;
    let y = MyBox::new(x);

    assert_eq!(5, x);
    assert_eq!(5, *y);  // to success compile, Deref definition should be defined

    println!("x = {}, y = {}", x, *y);

    let m = MyBox::new(String::from("Rust"));
    hello(&m);
}

 

admin

車の中に三日放置で変形

夏に近づいたせいか、車内の温度は相当上昇するようで、土曜日にデモのため持ち出したホーバークラフト、今日取り出してみるとファンのホルダーがグダグダに変形しています。

PLAの耐熱温度は60℃ぐらいらしいから、車内だと今でも直射日光浴びる場所だとアウトの模様。この先意識しておかないと、

ABSだと耐熱性は問題なくとも、形状安定性特にファンのホルダーのようなそこそこ寸法精度が必要とされる場所には向いていないと思う。

 

admin

ホバークラフト(ver.2)

ようやくver.2を製作した、

改良点は、

① プロペラを水平(可変はできる)取り付けで横方向の推力が出ないようにした

② プロペラの風の抜けをよくできるような取り付け形状に改良

③ micro:bitを水平に取り付けてコンパス機能を使えるようにした

④ 金属ネジとか使わないで、かつコネクタも軽量化して、全体で二割ぐらいは軽量化

ver.1のようにファンが傾いていないから、割と安定してホバリングしている。

プロペラ駆動力(回転数)の調整でどこまで移動や回転が制御できるかはこれから、

<micro:bit制御用のコード>

・本体用

https://github.com/chateight/hober

ここでファンに与える電圧(回転数)を変えれば動きが制御できるはず

・コントローラー用

https://github.com/chateight/hober_ctl

 

<モーター駆動回路図>

 

 

admin

関数呼び出しと戻り値がクロージャーの場合(@Rust)

https://kudohamu.hatenablog.com/entry/2015/09/26/084621

で、以下のようなコードが出てきて、なぜhoge()()と()が二重になるのかなと思いましたが、

fn main(){
    println!("{}", hoge()());
}

fn hoge() -> Box<dyn Fn() -> String> {
    Box::new(|| -> String {
        String::from("ほげー!")
    })
}

Geminiで質問すると、

hoge()だけだと関数呼び出すだけで、hoge()()としないと戻り値であるクロージャーの実行はできないと以下のように言われました。

まとめ:

hoge()() は、hoge 関数を呼び出し、その結果として得られたクロージャを呼び出すという二つの操作をまとめて実行しています。

「関数の戻り値としてのクロージャーを実行するなら二重括弧を使う」ということのようです

したがって、以下のように冗長に記述もできます。

    let abc = hoge();
    println!("{}", abc());

同じことは、クロージャーを戻り値にしている

https://isehara-3lv.sakura.ne.jp/blog/2024/04/05/クロージャーを戻り値にするrust/

のコードでも言えて、

    let fn_plain = create_fn(); // return a closure
    
    fn_plain();

と

    create_fn()();

は等価です

 

admin

Iterator::anyとfind

https://doc.rust-jp.rs/rust-by-example-ja/fn/closures/closure_examples/iter_any.html

と次のページに出てくるanyとfindですが、トレイトの型は以下のようになっていますが、findの&Self::Itemはクロージャが変数を参照として取ってくるので、クロージャーでの表現は二重参照の形になります。

<any/findのクレート表現>

    fn any<F>(&mut self, f: F) -> bool
    where
        Self: Sized,
        F: FnMut(Self::Item) -> bool,


    fn find<P>(&mut self, predicate: P) -> Option<self::item> 
    where 
        Self: Sized, 
        P: FnMut(&Self::Item) -> bool,  // &Self::Item means closure variables are used as reference  

以上の前提なので、

https://doc.rust-jp.rs/rust-by-example-ja/fn/closures/closure_examples/iter_find.html

からのソースコードでは&x -> &&x、x -> &xのように参照記号(&)が追加されています。

fn main() {
    let vec1 = vec![1, 2, 3];
    let vec2 = vec![4, 5, 6];

    // `iter()` for vecs yields `&i32`.
    let mut iter = vec1.iter();
    // `into_iter()` for vecs yields `i32`.
    let mut into_iter = vec2.into_iter();

    // `iter()` for vecs yields `&i32`, and we want to reference one of its
    // items, so we have to destructure `&&i32` to `i32`
    println!("Find 2 in vec1: {:?}", iter     .find(|&&x| x == 2));
    // `into_iter()` for vecs yields `i32`, and we want to reference one of
    // its items, so we have to destructure `&i32` to `i32`
    println!("Find 2 in vec2: {:?}", into_iter.find(| &x| x == 2));

    let array1 = [1, 2, 3];
    let array2 = [4, 5, 6];

    // `iter()` for arrays yields `&i32`
    println!("Find 2 in array1: {:?}", array1.iter()     .find(|&&x| x == 2));
    // `into_iter()` for arrays yields `i32`
    println!("Find 2 in array2: {:?}", array2.into_iter().find(|&x| x == 2));
}

 

admin

クロージャーを関数の戻り値にする(@rust)

クロージャーを関数の戻り値にするときにも、Fn/FnMut/FnOnce指定が必要です。なぜならクロージャーの受け取りと同じくクロージャーの型は不明なので、

さらに関数の戻り値とするためにはクロージャーに渡される変数をmoveで参照では無く値渡しにしないといけません。以下の例でdataはクロージャーに渡しても戻り値では無い(参照が無効になることはない)ので、ここでのmoveはあっても無くても、値渡しだと時間が余計にかかるだけで、結果は同じですが、create_fn()中のクロージャーにはmoveがないとtextが参照では関数の戻りで参照無効となるのでコンパイルが通りません。

// return a closure
fn create_fn() -> impl Fn() {
    let text = "Fn".to_owned();
    move || println!("This is a: {}", text)
}

fn main() {
    // return a closure
    let data = vec![1, 2, 3];

    // you don't need 'move' in this case becuase data is still available after this line
    let closure = move || println!("captured {data:?} by value");   
    closure();

    // call function and receive closure
    let fn_plain = create_fn(); // return a closure
    
    fn_plain();

 

admin

 

関数に関数を渡す(@Rust)

関数の引数としてクロージャーを渡せるのと同様に関数も引数として渡すことができます。

以下がサンプルのコードですが、ここではクロージャー(sqr)と関数(sqr_f)とそれらの引数も同時に関数(apply_clo())に渡しています。

当然トレイト境界と引数がクロージャーと同じ関数(ここではFn(i32))でないと渡せませんが、


fn apply_clo<F: Fn(i32)>(i: i32, f: F) {
    f(i);
}

fn main() {
    // ARG for the closure
    let sqr = |y: i32| println!("{}", y*y);

    // equivalent function
    fn sqr_f(y: i32){
        println!("{}", y*y);
    }

    apply_clo(23, sqr);
    apply_clo(32, sqr_f);
}

 

admin

 

クロージャーとFn/FnMut/FnOnceの使い分け(@Rust)

これもRustのメモリ管理のための特有機能になりますが、クロージャーを使う時にはその引数は、匿名構造体に保存されるようで、クロージャーを受け取るときにその引数がどういう使われ方をするかを指定するためにFn/FnMut/FnOnceの三種類のトレイトが存在します。

Fn/FnMut/FnOnceについての解説は以下のリンクが分かりやすい。

https://qiita.com/hiratasa/items/c1735dc4c7c78b0b55e9

Rust by exampleの以下のリンクから、

https://doc.rust-jp.rs/rust-by-example-ja/fn/closures/input_parameters.html

コードを抜き出すと以下のようになりますが、ここでfn apply()とfn apply_to_3()はお互いに独立しています。

fn apply_to_3()はトレイト境界がシンプルで中で何らの変更もしていないのでFn指定になっています。

// A function which takes a closure as an argument and calls it.
// <f> denotes that F is a "Generic type parameter"
fn apply<f>(f: F) where
    // The closure takes no input and returns nothing.
    F: FnOnce() {
    // ^ TODO: Try changing this to `Fn` or `FnMut`.

    f();
}

// A function which takes a closure and returns an `i32`.
fn apply_to_3<f>(f: F) -> i32 where
    // The closure takes an `i32` and returns an `i32`.
    F: Fn(i32) -> i32 {

    f(3)
}

fn main() {
    use std::mem;

    let greeting = "hello";
    // A non-copy type.
    // `to_owned` creates owned data from borrowed one
    let mut farewell = "goodbye".to_owned();

    // Capture 2 variables: `greeting` by reference and
    // `farewell` by value.
    let diary = || {
        // `greeting` is by reference: requires `Fn`.
        println!("I said {}.", greeting);

        // Mutation forces `farewell` to be captured by
        // mutable reference. Now requires `FnMut`.
        farewell.push_str("!!!");
        println!("Then I screamed {}.", farewell);
        println!("Now I can sleep. zzzzz");

        // Manually calling drop forces `farewell` to
        // be captured by value. Now requires `FnOnce`.
        mem::drop(farewell);
    };

    // Call the function which applies the closure.
    apply(diary);

    // `double` satisfies `apply_to_3`'s trait bound
    let double = |x| 2 * x;

    println!("3 doubled: {}", apply_to_3(double));
}

Fn/FnMut/FnOnceの三種類のトレイトは、それぞれ以下のようになっていてFnMutはFnOnceをFnはFnMutをそれぞれ継承しています。それぞれの関数の引数を見ると、引数をどのように扱っているかが分かりますが一番過激なのがFnOnce、ただしdropしてしまうので一度しか使えない、変更だけならFnMut、参照するだけならFnというふうになります。

上記のコードでmem::drop(farewell);がなければ、F: FnOnce()はFnMutで問題無いし(ただしfn apply<f>(f: F) whereはfn apply<f>(mut f: F) whereに変更必要)farewell.push_str("!!!");がなければ(つまり引数を変更しなければ)Fnで問題ありません。

pub trait FnOnce
where
    Args: Tuple,
{
    type Output;

    // Required method
    extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

pub trait FnMut: FnOnce
where
    Args: Tuple,
{
    // Required method
    extern "rust-call" fn call_mut(
        &mut self,
        args: Args
    ) -> Self::Output;
}

pub trait Fn: FnMut
where
    Args: Tuple,
{
    // Required method
    extern "rust-call" fn call(&self, args: Args) -> Self::Output;
}

 

admin