031 the VEC implementation of rust Necromancer’s book insert and remove

Time:2021-8-27

introduce

The videos recorded in this series are mainly put on station BRust dead book learning video

The source code information related to rust dead book is ingithub.com/anonymousGiga/Rustonomi…

Details

The following implements the insert and remove functions for myvec:

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

#[derive(Debug)]
pub struct MyVec<T> {
    ptr: Unique<T>,
    cap: usize,
    len: usize,
}

impl<T> MyVec<T> {
    fn new() -> Self {
        assert!( mem::size_ of::<T>() !=  0, "not ready" (zero size type);
        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(); 
        }

        //The key point is to overwrite directly, because I don't know if there is anything before the memory
        unsafe {
            ptr::write(self.ptr.as_ptr().offset(self.len as isize), elem);
        }

        self.len += 1;
    }

    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 insert(&mut self, index: usize, elem: T) {
        assert!( Index < = self.len, "out of bounds");
        if self.cap == self.len { 
            self.grow(); 
        }

        unsafe {
            if index < self.len {
                ptr::copy(self.ptr.as_ptr().offset(index as isize),
                        self.ptr.as_ptr().offset((index as isize) + 1),
                        self.len - index);
            }
            ptr::write(self.ptr.as_ptr().offset(index as isize), elem);
            self.len += 1;
        }
    }

    fn remove(&mut self, index: usize) -> T {
        assert!( Index < self.len, "out of bounds");
        unsafe {
            self.len -= 1;
            let result = ptr::read(self.ptr.as_ptr().offset(index as isize));
            ptr::copy(self.ptr.as_ptr().offset(index as isize + 1), 
                    self.ptr.as_ptr().offset(index as isize),
                    self.len - index);
            result
        }
    }
}

impl<T> Drop for MyVec<T> {
    fn drop(&mut self) {
        if self.cap != 0 {
            while let Some(_) = self.pop() {}

            let align = mem::align_of::<T>();
            let elem_size = mem::size_of::<T>();
            let num_bytes = elem_size * self.cap;

            unsafe {
                let layout: Layout = Layout::from_size_align_unchecked(num_bytes, align);
                dealloc(self.ptr.as_ptr() as *mut _, layout)
            }

            println!("release memory in drop function!");
        }
    }
}

impl<T> Deref for MyVec<T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        unsafe {
            slice::from_raw_parts(self.ptr.as_ptr(), self.len)
        }
    }
}

impl<T> DerefMut for MyVec<T> {
    fn deref_mut(&mut self) -> &mut [T] {
        unsafe {
            slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len)
        }
    }
}

fn main() {

    {
        let mut vec: MyVec<i32> = MyVec::new();
        vec.push(8);
        vec.push(7);
        vec.push(6);

        while let Some(v) = vec.pop() {
            println!("v == {}", v);
        }

        vec.push(8);
        vec.push(7);
        vec.push(6);

        let s = &vec[1..];
        println!("s[0] == {}", s[0]);

        let s = &mut vec[1..];
        s[0] = 10;
        println!("s[0] == {}", s[0]);

        println!("-------------------------------");
        let mut vec2: MyVec<i32> = MyVec::new();
        vec2.push(1);
        vec2.push(2);
        vec2.push(3);
        //Print
        //while let Some(v) = vec2.pop() {
        //    println!("v == {}", v);
        //}


        vec2.insert(1, 11);
        let ret = vec2.remove(2);
        println!("remove elem: {}", ret);
        //Print
        while let Some(v) = vec2.pop() {
            println!("v == {}", v);
        }
    }

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

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Linghu rushed