Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/servo/components/style/rule_tree/   (Firefox Browser Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 1 kB image not shown  

Quelle  unsafe_box.rs

  Sprache: Rust
 

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */


#![allow(unsafe_code)]

use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::ptr;

/// An unsafe box, derefs to `T`.
pub(superstruct UnsafeBox<T> {
    inner: ManuallyDrop<Box<T>>,
}

impl<T> UnsafeBox<T> {
    /// Creates a new unsafe box.
    pub(superfn from_box(value: Box<T>) -> Self {
        Self {
            inner: ManuallyDrop::new(value),
        }
    }

    /// Creates a new box from a pointer.
    ///
    /// # Safety
    ///
    /// The input should point to a valid `T`.
    pub(superunsafe fn from_raw(ptr: *mut T) -> Self {
        Self {
            inner: ManuallyDrop::new(Box::from_raw(ptr)),
        }
    }

    /// Creates a new unsafe box from an existing one.
    ///
    /// # Safety
    ///
    /// There is no refcounting or whatever else in an unsafe box, so this
    /// operation can lead to double frees.
    pub(superunsafe fn clone(this: &Self) -> Self {
        Self {
            inner: ptr::read(&this.inner),
        }
    }

    /// Returns a mutable reference to the inner value of this unsafe box.
    ///
    /// # Safety
    ///
    /// Given `Self::clone`, nothing prevents anyone from creating
    /// multiple mutable references to the inner value, which is completely UB.
    pub(crateunsafe fn deref_mut(this: &mut Self) -> &mut T {
        &mut this.inner
    }

    /// Drops the inner value of this unsafe box.
    ///
    /// # Safety
    ///
    /// Given this doesn't consume the unsafe box itself, this has the same
    /// safety caveats as `ManuallyDrop::drop`.
    pub(superunsafe fn drop(this: &mut Self) {
        ManuallyDrop::drop(&mut this.inner)
    }
}

impl<T> Deref for UnsafeBox<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

Messung V0.5 in Prozent
C=93 H=99 G=95

¤ Dauer der Verarbeitung: 0.13 Sekunden  (vorverarbeitet am  2026-06-20) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.