028 Rust死灵书之Vec的push和pop

Rust
263
0
0
2022-04-12

介绍

本系列录制的视频主要放在B站上Rust死灵书学习视频

Rust 死灵书相关的源码资料在github.com/anonymousGiga/Rustonomi...

Vec的push和pop

本节给MyVec实现push和pop方法,完整代码如下:

#![feature(ptr_internals)]
use std::mem;
use std::alloc::{alloc, realloc, Layout, handle_alloc_error};
use std::ptr::{Unique, self};

pub struct MyVec<T> {
    ptr: Unique<T>,
    cap: usize,
    len: usize,
}

impl<T> MyVec<T> {
    fn new() -> Self {
        assert!(mem::size_of::<T>() != 0, "还没准备好处理零尺寸类型");
        MyVec { ptr: Unique::dangling(), len: 0, cap: 0 }}

    fn grow(&mut self) {
        unsafe {let align = mem::align_of::<T>();let elem_size = mem::size_of::<T>();let layout: Layout;

            let (new_cap, ptr) = if self.cap == 0 {
                layout = Layout::from_size_align_unchecked(elem_size, align);let ptr = alloc(layout);(1, ptr)} else {let new_cap = self.cap * 2;let old_num_bytes = self.cap * elem_size;

                assert!(old_num_bytes <= (isize::MAX as usize) / 2,"capacity overflow");


                let new_num_bytes = old_num_bytes * 2;
                layout = Layout::from_size_align_unchecked(new_num_bytes, align);let ptr = realloc(self.ptr.as_ptr() as *mut _,
                                      layout,
                                    new_num_bytes);(new_cap, ptr)};

            if ptr.is_null() { handle_alloc_error(layout); }

            if let Some(ptr) = Unique::new(ptr as *mut _) {
                self.ptr = ptr;} else {
                panic!("error!");}
            self.cap = new_cap;}}

    fn push(&mut self, elem: T) {if self.len == self.cap { 
            self.grow(); 
        }

        //关键点在于要直接覆盖,因为不知道内存之前是否有东西 
        unsafe {
            ptr::write(self.ptr.as_ptr().offset(self.len as isize), elem);}

        self.len += 1;}

    pub fn pop(&mut self) -> Option<T> {if self.len == 0 {
            None
        } else {
            self.len -= 1;
            unsafe {                       
                Some(ptr::read(self.ptr.as_ptr().offset(self.len as isize)))}}}
}

fn main() {let mut vec: MyVec<i32> = MyVec::new();
    vec.push(8);if let Some(v) = vec.pop() {
        println!("v == {}", v);}

    println!("Hello, world!");
}

关键点说明:

实现pop的时候,需要对内存直接写入。