実例: !Unpin 型に Drop を実装する

// 著作権 2026 Google LLC
// SPDX-License-Identifier: Apache-2.0

use std::cell::RefCell;
use std::marker::PhantomPinned;
use std::mem;
use std::pin::Pin;

thread_local! {
    static BATCH_FOR_PROCESSING: RefCell<Vec<String>> = RefCell::new(Vec::new());
}

#[derive(Debug)]
struct CustomString(String);

#[derive(Debug)]
struct SelfRef {
    data: CustomString,
    ptr: *const CustomString,
    _pin: PhantomPinned,
}

impl SelfRef {
    fn new(data: &str) -> Pin<Box<SelfRef>> {
        let mut boxed = Box::pin(SelfRef {
            data: CustomString(data.to_owned()),
            ptr: std::ptr::null(),
            _pin: PhantomPinned,
        });

        let ptr: *const CustomString = &boxed.data;
        unsafe {
            Pin::get_unchecked_mut(Pin::as_mut(&mut boxed)).ptr = ptr;
        }
        boxed
    }
}

impl Drop for SelfRef {
    fn drop(&mut self) {
        // SAFETY: String からバイトを読み取っているため安全
        let payload = unsafe { std::ptr::read(&self.data) };
        BATCH_FOR_PROCESSING.with(|log| log.borrow_mut().push(payload.0));
    }
}

fn main() {
    let pinned = SelfRef::new("Rust 🦀");
    drop(pinned);

    BATCH_FOR_PROCESSING.with(|batch| {
        println!("Batch: {:?}", batch.borrow());
    });
}

この例では、テレメトリやロギングなどの後処理のためのデータを追加するために Drop トレイトを使用しています。

この Safety コメントは誤っています。 ptr::read はビット単位のコピーを作成するため、self.data は無効な状態のままになります。self.data はメソッドの末尾でもう一度ドロップされるため、二重解放になります。

クラスにコードを修正してもらってください。

提案 0: 再設計

後処理システムが Drop を使わずに動作するように再設計してください。

提案 1: Clone

.clone() を使うのは最初の明白な選択肢ですが、メモリを確保します。

// 著作権 2026 Google LLC
// SPDX-License-Identifier: Apache-2.0

impl Drop for SelfRef {
    fn drop(&mut self) {
        let payload = self.data.0.clone();
        BATCH_FOR_PROCESSING.with(|log| log.borrow_mut().push(payload));
    }
}

提案 2: ManuallyDrop

CustomStringManuallyDrop でラップすると、Drop 実装の末尾での(2 回目の)自動ドロップを防げます。

// 著作権 2026 Google LLC
// SPDX-License-Identifier: Apache-2.0

struct SelfRef {
    data: ManuallyDrop<CustomString>,
    ptr: *const CustomString,
    _pin: PhantomPinned,
}

// ...

impl Drop for SelfRef {
    fn drop(&mut self) {
        // SAFETY: self.data
        let payload = unsafe { ManuallyDrop::take(&mut self.data) };
        BATCH_FOR_PROCESSING.with(|log| log.borrow_mut().push(payload.0));
    }
}