Redoxコードリーディングメモ
forループの型
code:log.rs
pub fn write(&mut self, buf: &u8) { for &b in buf {
&bはどのような効果があるのか
→パターンマッチにより、bをu8とする効果
for b in bufとするとbは&u8になる
bが参照でなくなると動作効率が上がったりするのだろうか?
LogとRedoxLoggerの役割の違い
::log::Logとは
impl ::log::Log for RedoxLogger {
::log::Logはどこで定義されている?
::logの::の効果は?
editionで解釈が異なる
関数ポインタ
code:Rust
struct RedoxLogger {
log_func: fn(&log::Record),
log_funcは関数
log_funcは関数の機械語を含む?
それとも1ワードの関数ポインタ?
関数ポインタの呼び出し
code:実験.rs
struct A {
f: fn(i64) -> i64,
}
code:C言語形式の呼び出しはエラー
13 | println!("a.f(3) = {}", a.f(3));
| ^ field, not a method
(a.f)(3)と書く必要がある
size_of::<A>()は8
size_of::<A>()の結果はどこ?
code:Rust
let siz = size_of::<A>();
println!("size of A = {}", siz);
code:逆アセンブル結果
let siz = size_of::<A>();
8005: 48 c7 44 24 70 08 00 mov QWORD PTR rsp+0x70,0x8 800c: 00 00
println!("size of A = {}", siz);
8013: e8 c8 fc ff ff call 7ce0 <core::fmt::ArgumentV1::new_display>
……
定数「8」が埋め込まれた!
code:インラインで呼び出した場合
println!("size of A = {}", size_of::<A>());
8005: 48 8d 3d 9c 50 03 00 lea rdi,rip+0x3509c # 3d0a8 <str.0+0x38> 800c: e8 cf fc ff ff call 7ce0 <core::fmt::ArgumentV1::new_display>
……
どこを探しても「8」が見つからない
code:.rodataのダンプ
Hex dump of section '.rodata':
……
0x0003d0a0 41203d20 00000000 08000000 00000000 A = ............
……
「8」があった!