How does Rust know what types of native resources?

When you have a pointer to a field for some memory allocated by the heap, I assume that Rust has “hard-coded” property knowledge, so when ownership is transferred by calling some function, the resources are moved, and the argument is in the function of the new owner.

However, how does this happen for vectors, for example? They also “own” their resources, and the mechanics of ownership apply the same as for pointers to the field — but they are regular values ​​stored in the variables themselves, not pointers. How does Rust (know to) apply ownership mechanics in this situation?

Can I create my own type that owns the resources?

+4
source share
2 answers

tl; dr: "owning" types in Rust is not some kind of magic, and they are certainly not hardcoded into the compiler or language. They are only types that are written in a certain way (they do not implement Copyand probably have a destructor), and they have a certain semantics that are provided through incompatibility and a destructor.

In its basic ownership mechanism, Rust is very simple and has very simple rules.

First of all, determine what a movement is. It's simple - the value is considered moved when it becomes available under the new name and ceases to be available under the old name:

struct X(u32);
let x1 = X(12);
let x2 = x1;
// x1 is no longer accessible here, trying to use it will cause a compiler error

The same thing happens when you pass a value to a function:

fn do_something(x: X) {}

let x1 = X(12);
do_something(x1);
// x1 is no longer accessible here

, - , , . , - , .

, , , , . , , Vec Box, . Drop trait:

struct X(u32);

impl Drop for X {
    fn drop(&mut self) {
        println!("Dropping {}", x.0);
    }
}

{
    let x1 = X(12);
}  // x1 is dropped here, and "Dropping 12" will be printed

, Copy trait, - , :

#[derive(Copy, Clone)] struct X(u32);
let x1 = X(12);
let x2 = x1;
// x1 is still available here

- x2 x1.

Copy - , Copy Drop. ( &mut, *const *mut raw ) Rust, , , Copy. , , Vec Box, Copy - , , .

Copy - , . Rust . , - -, , "Box<T> T", , , . , Vec Box, Copy , , , () , , (, , ..).

, "" . Rust, . , C API . "" Rust , , , :

extern {
    fn create_widget() -> *mut WidgetStruct;
    fn destroy_widget(w: *mut WidgetStruct);
    fn use_widget(w: *mut WidgetStruct) -> u32;
}

struct Widget(*mut WidgetStruct);

impl Drop for Widget {
    fn drop(&mut self) {
        unsafe { destroy_widget(self.0); }
    }
}

impl Widget {
    fn new() -> Widget { Widget(unsafe { create_widget() }) }

    fn use_it(&mut self) -> u32 {
        unsafe { use_widget(self.0) }
    }
}

, Widget , *mut WidgetStruct.

+12

, :

extern crate libc;

use libc::{malloc, free, c_void};


struct OwnerOfMemory {
    ptr: *mut c_void
}

impl OwnerOfMemory {
    fn new() -> OwnerOfMemory {
        OwnerOfMemory {
            ptr: unsafe { malloc(128) }
        }
    }
}

impl Drop for OwnerOfMemory {
    fn drop(&mut self) {
        unsafe { free(self.ptr); }
    }
}

fn main() {
    let value = OwnerOfMemory::new();
}
+2

Source: https://habr.com/ru/post/1599126/


All Articles