欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Rust中的胖指針怎么使用

這篇文章主要講解了“Rust中的胖指針怎么使用”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Rust中的胖指針怎么使用”吧!

10年積累的成都網(wǎng)站設計、做網(wǎng)站經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先網(wǎng)站設計后付款的網(wǎng)站建設流程,更有于都免費網(wǎng)站建設讓你可以放心的選擇與我們合作。

喚醒器

Waker  類型允許在運行時的reactor 部分和執(zhí)行器部分之間進行松散耦合。
通過使用不與  Future  執(zhí)行綁定的喚醒機制,運行時實現(xiàn)者可以提出有趣的新喚醒機制。例如,可以生成一個線程來執(zhí)行一些工作,這些工作結束時通知  Future  ,這完全獨立于當前的運行時。
如果沒有喚醒程序,執(zhí)行程序將是通知正在運行的任務的唯一方式,而使用喚醒程序,我們將得到一個松散耦合,其中很容易使用新的  leaf-future  來擴展生態(tài)系統(tǒng)。
如果你想了解更多關于 Waker 類型背后的原因,我可以推薦   Withoutboats articles series about them   。  

理解喚醒器

在實現(xiàn)我們自己的  Future  時,我們遇到的最令人困惑的事情之一就是我們?nèi)绾螌崿F(xiàn)一個喚醒器。創(chuàng)建一個 Waker 需要創(chuàng)建一個 vtable,這個vtable允許我們使用動態(tài)方式調(diào)用我們真實的Waker實現(xiàn).
如果你想知道更多關于Rust中的動態(tài)分發(fā),我可以推薦 Adam Schwalm 寫的一篇文章     Exploring Dynamic Dispatch in Rust   .  
讓我們更詳細地解釋一下。

Rust中的胖指針

為了更好地理解我們?nèi)绾卧?Rust 中實現(xiàn) Waker,我們需要退后一步并討論一些基本原理。讓我們首先看看 Rust 中一些不同指針類型的大小。
運行以下代碼:

trait SomeTrait { }
fn main() {    println!("======== The size of different pointers in Rust: ========");    println!("&dyn Trait:-----{}", size_of::<&dyn SomeTrait>());    println!("&[&dyn Trait]:--{}", size_of::<&[&dyn SomeTrait]>());    println!("Box<Trait>:-----{}", size_of::<Box<SomeTrait>>());    println!("&i32:-----------{}", size_of::<&i32>());    println!("&[i32]:---------{}", size_of::<&[i32]>());    println!("Box<i32>:-------{}", size_of::<Box<i32>>());    println!("&Box<i32>:------{}", size_of::<&Box<i32>>());    println!("[&dyn Trait;4]:-{}", size_of::<[&dyn SomeTrait; 4]>());    println!("[i32;4]:--------{}", size_of::<[i32; 4]>());}
從運行后的輸出中可以看到,引用的大小是不同的。許多是8字節(jié)(在64位系統(tǒng)中是指針大小) ,但有些是16字節(jié)。
16字節(jié)大小的指針被稱為“胖指針” ,因為它們攜帶額外的信息。
例如    &[i32]  :
  • 前8個字節(jié)是指向數(shù)組中第一個元素的實際指針(或 slice 引用的數(shù)組的一部分)
  • 第二個8字節(jié)是切片的長度
例如    &dyn SomeTrait  :
這就是我們將要關注的胖指針的類型。  &dyn SomeTrait   是一個trait的引用,或者 Rust稱之為一個trait對象。
指向 trait 對象的指針布局如下:
  • 前8個字節(jié)指向trait 對象的data
  • 后八個字節(jié)指向trait對象的 vtable
這樣做的好處是,我們可以引用一個對象,除了它實現(xiàn)了 trait 定義的方法之外,我們對這個對象一無所知。為了達到這個目的,我們使用動態(tài)分發(fā)。
讓我們用代碼而不是文字來解釋這一點,通過這些部分來實現(xiàn)我們自己的 trait 對象:

// A reference to a trait object is a fat pointer: (data_ptr, vtable_ptr)trait Test {    fn add(&self) -> i32;    fn sub(&self) -> i32;    fn mul(&self) -> i32;}
// This will represent our home brewn fat pointer to a trait object   #[repr(C)]struct FatPointer<'a> {    /// A reference is a pointer to an instantiated `Data` instance    data: &'a mut Data,    /// Since we need to pass in literal values like length and alignment it's    /// easiest for us to convert pointers to usize-integers instead of the other way around.    vtable: *const usize,}
// This is the data in our trait object. It's just two numbers we want to operate on.struct Data {    a: i32,    b: i32,}
// ====== function definitions ======fn add(s: &Data) -> i32 {    s.a + s.b}fn sub(s: &Data) -> i32 {    s.a - s.b}fn mul(s: &Data) -> i32 {    s.a * s.b}
fn main() {    let mut data = Data {a: 3, b: 2};    // vtable is like special purpose array of pointer-length types with a fixed    // format where the three first values has a special meaning like the    // length of the array is encoded in the array itself as the second value.    let vtable = vec![        0,            // pointer to `Drop` (which we're not implementing here)        6,            // lenght of vtable        8,            // alignment
       // we need to make sure we add these in the same order as defined in the Trait.        add as usize, // function pointer - try changing the order of `add`        sub as usize, // function pointer - and `sub` to see what happens        mul as usize, // function pointer    ];
   let fat_pointer = FatPointer { data: &mut data, vtable: vtable.as_ptr()};    let test = unsafe { std::mem::transmute::<FatPointer, &dyn Test>(fat_pointer) };
   // And voalá, it's now a trait object we can call methods on    println!("Add: 3 + 2 = {}", test.add());    println!("Sub: 3 - 2 = {}", test.sub());    println!("Mul: 3 * 2 = {}", test.mul());}

感謝各位的閱讀,以上就是“Rust中的胖指針怎么使用”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Rust中的胖指針怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!

當前文章:Rust中的胖指針怎么使用
標題來源:http://www.chinadenli.net/article36/jsgdpg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版App開發(fā)做網(wǎng)站響應式網(wǎng)站域名注冊用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化