這篇文章主要講解了“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
,這完全獨立于當前的運行時。leaf-future
來擴展生態(tài)系統(tǒng)。如果你想了解更多關于 Waker 類型背后的原因,我可以推薦 Withoutboats articles series about them 。
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 .
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]>());
}
&[i32]
:&dyn SomeTrait
:&dyn SomeTrait
是一個trait的引用,或者 Rust稱之為一個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)