forked from rust-lang/nomicon
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
destructors.md 에서 마지막 코드 섹션이 좀 이상하다. 이렇게 쓸 필요가 있나? 이렇게 써도 되지 않을까?
#![feature(allocator_api, ptr_internals)]
use std::alloc::{Allocator, Global, Layout};
use std::mem::ManuallyDrop;
use std::ptr::{drop_in_place, NonNull, Unique};
struct MyBox<T> {
ptr: Unique<T>,
}
impl<T> Drop for MyBox<T> {
fn drop(&mut self) {
unsafe {
drop_in_place(self.ptr.as_ptr());
let c: NonNull<T> = self.ptr.into();
Global.deallocate(c.cast(), Layout::new::<T>());
}
}
}
struct SuperBox<T> {
my_box: ManuallyDrop<MyBox<T>>,
}
impl<T> Drop for SuperBox<T> {
fn drop(&mut self) {
unsafe {
let c: NonNull<T> = self.my_box.ptr.into();
Global.deallocate(c.cast(), Layout::new::<T>());
}
}
}
fn main() {}일단 보이기로는 ManuallyDrop이 재귀적인 해제를 막아주는 것처럼 보인다...
Metadata
Metadata
Assignees
Labels
No labels