Bug 1716535 - Remove nodrop dependency. r=glandium

ManuallyDrop is just fine.

Differential Revision: https://phabricator.services.mozilla.com/D117887
This commit is contained in:
Emilio Cobos Álvarez 2021-06-16 09:48:14 +00:00
Родитель 680ed2d221
Коммит a22ca7007a
7 изменённых файлов: 4 добавлений и 326 удалений

7
Cargo.lock сгенерированный
Просмотреть файл

@ -3430,12 +3430,6 @@ dependencies = [
"void",
]
[[package]]
name = "nodrop"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "nom"
version = "5.1.2"
@ -4600,7 +4594,6 @@ dependencies = [
name = "servo_arc"
version = "0.1.1"
dependencies = [
"nodrop",
"stable_deref_trait",
]

Просмотреть файл

@ -15,6 +15,5 @@ gecko_refcount_logging = []
servo = ["serde"]
[dependencies]
nodrop = { version = "0.1.8" }
serde = { version = "1.0", optional = true }
stable_deref_trait = "1.0.0"

Просмотреть файл

@ -25,12 +25,10 @@
// duplicate those here.
#![allow(missing_docs)]
extern crate nodrop;
#[cfg(feature = "servo")]
extern crate serde;
extern crate stable_deref_trait;
use nodrop::NoDrop;
#[cfg(feature = "servo")]
use serde::{Deserialize, Serialize};
use stable_deref_trait::{CloneStableDeref, StableDeref};
@ -305,7 +303,7 @@ impl<T> Arc<T> {
F: FnOnce(&RawOffsetArc<T>) -> U,
{
// Synthesize transient Arc, which never touches the refcount of the ArcInner.
let transient = unsafe { NoDrop::new(Arc::into_raw_offset(ptr::read(self))) };
let transient = unsafe { mem::ManuallyDrop::new(Arc::into_raw_offset(ptr::read(self))) };
// Expose the transient Arc to the callback, which may clone it if it wants.
let result = f(&transient);
@ -926,7 +924,7 @@ impl<H, T> ThinArc<H, T> {
{
// Synthesize transient Arc, which never touches the refcount of the ArcInner.
let transient = unsafe {
NoDrop::new(Arc {
mem::ManuallyDrop::new(Arc {
p: ptr::NonNull::new_unchecked(thin_to_thick(self.ptr.as_ptr())),
phantom: PhantomData,
})
@ -935,11 +933,6 @@ impl<H, T> ThinArc<H, T> {
// Expose the transient Arc to the callback, which may clone it if it wants.
let result = f(&transient);
// Forget the transient Arc to leave the refcount untouched.
// XXXManishearth this can be removed when unions stabilize,
// since then NoDrop becomes zero overhead
mem::forget(transient);
// Forward the result.
result
}
@ -1140,16 +1133,11 @@ impl<T> RawOffsetArc<T> {
F: FnOnce(&Arc<T>) -> U,
{
// Synthesize transient Arc, which never touches the refcount of the ArcInner.
let transient = unsafe { NoDrop::new(Arc::from_raw(self.ptr.as_ptr())) };
let transient = unsafe { mem::ManuallyDrop::new(Arc::from_raw(self.ptr.as_ptr())) };
// Expose the transient Arc to the callback, which may clone it if it wants.
let result = f(&transient);
// Forget the transient Arc to leave the refcount untouched.
// XXXManishearth this can be removed when unions stabilize,
// since then NoDrop becomes zero overhead
mem::forget(transient);
// Forward the result.
result
}
@ -1272,16 +1260,11 @@ impl<'a, T> ArcBorrow<'a, T> {
T: 'static,
{
// Synthesize transient Arc, which never touches the refcount.
let transient = unsafe { NoDrop::new(Arc::from_raw(self.0)) };
let transient = unsafe { mem::ManuallyDrop::new(Arc::from_raw(self.0)) };
// Expose the transient Arc to the callback, which may clone it if it wants.
let result = f(&transient);
// Forget the transient Arc to leave the refcount untouched.
// XXXManishearth this can be removed when unions stabilize,
// since then NoDrop becomes zero overhead
mem::forget(transient);
// Forward the result.
result
}

Просмотреть файл

@ -1 +0,0 @@
{"files":{"Cargo.toml":"e43d9892df1d8d932b5f5d27f303f1a302b550b5a37b9b8fb1b107aa5273b8af","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"0245ee104228a100ce5fceecf43e25faae450494d9173f43fd94c27d69fdac13","README.rst":"3a12c4caf645c40c7b9608c150c7e2bee50eafd114b802329f508a489d9e175b","src/lib.rs":"3e00173987c5fc462956c36f492426cbf0d900a2634c6b4e47580d6b142273ee"},"package":"72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"}

36
third_party/rust/nodrop/Cargo.toml поставляемый
Просмотреть файл

@ -1,36 +0,0 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies
#
# If you believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package]
name = "nodrop"
version = "0.1.14"
authors = ["bluss"]
description = "A wrapper type to inhibit drop (destructor).\n\n***Deprecated: Use ManuallyDrop or MaybeUninit instead!***\n"
documentation = "https://docs.rs/nodrop/"
keywords = []
categories = ["rust-patterns"]
license = "MIT/Apache-2.0"
repository = "https://github.com/bluss/arrayvec"
[package.metadata.release]
no-dev-version = true
tag-name = "nodrop-{{version}}"
[dependencies.nodrop-union]
version = "0.1.8"
optional = true
[features]
default = ["std"]
std = []
use_needs_drop = []
use_union = ["nodrop-union"]
[badges.maintenance]
status = "deprecated"

71
third_party/rust/nodrop/README.rst поставляемый
Просмотреть файл

@ -1,71 +0,0 @@
nodrop
======
Recent Changes (nodrop)
-----------------------
- 0.1.14
- Mark nodrop deprecated
With the release of arrayvec 0.5, nodrop is unused.
With the release of Rust 1.36 and MaybeUninit, nodrop has no
purpose (but for older Rust releases).
- 0.1.13
- Update to include license files in the crate by @ignatenkobrain
- 0.1.12
- Remove dependency on crate odds.
- 0.1.11
- Remove erronous assertion in test (#77)
- 0.1.10
- Update for stable ``needs_drop`` (Rust 1.21, was nightly only)
- 0.1.9
- Fix issue in recent nightly where ``repr(u8)`` did not work. Use
a better way to get rid of the enum layout optimization.
- 0.1.8
- Add crate feature ``use_union`` that uses untagged unions to implement NoDrop.
Finally we have an implementation without hacks, without a runtime flag,
and without an actual ``Drop`` impl (which was needed to suppress drop).
The crate feature requires nightly and is unstable.
- 0.1.7
- Remove crate feature ``no_drop_flag``, because it doesn't compile on nightly
anymore. Drop flags are gone anyway!
- 0.1.6
- Add feature std, which you can opt out of to use ``no_std``.
- 0.1.5
- Added crate feature ``use_needs_drop`` which is a nightly-only
optimization, which skips overwriting if the inner value does not need
drop.
License
=======
Dual-licensed to be compatible with the Rust project.
Licensed under the Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0 or the MIT license
http://opensource.org/licenses/MIT, at your
option. This file may not be copied, modified, or distributed
except according to those terms.

189
third_party/rust/nodrop/src/lib.rs поставляемый
Просмотреть файл

@ -1,189 +0,0 @@
//! A wrapper type to inhibit drop (destructor).
//!
//! ***Deprecated: Use ManuallyDrop or MaybeUninit instead!***
//!
//! The **nodrop** crate has the following cargo feature flags:
//!
//! - `std`
//! - Optional, enabled by default
//! - Use libstd
//! - `use_needs_drop`
//! - Optional
//! - Requires Rust 1.21
//! - Use `needs_drop` to skip overwriting if not necessary
//! - `use_union`
//! - Optional
//! - Requires nightly channel
//! - Using untagged union, finally we have an implementation of `NoDrop` without hacks,
//! for example the fact that `NoDrop<T>` never has a destructor anymore.
//!
#![cfg_attr(not(any(test, feature="std")), no_std)]
#[cfg(not(any(test, feature="std")))]
extern crate core as std;
#[cfg(feature = "use_union")]
extern crate nodrop_union as imp;
pub use imp::NoDrop;
#[cfg(not(feature = "use_union"))]
mod imp {
use std::ptr;
use std::mem;
use std::ops::{Deref, DerefMut};
/// repr(u8) - Make sure the non-nullable pointer optimization does not occur!
#[repr(u8)]
enum Flag<T> {
Alive(T),
// Dummy u8 field below, again to beat the enum layout opt
Dropped(u8),
}
/// A type holding **T** that will not call its destructor on drop
pub struct NoDrop<T>(Flag<T>);
impl<T> NoDrop<T> {
/// Create a new **NoDrop**.
#[inline]
pub fn new(value: T) -> NoDrop<T> {
NoDrop(Flag::Alive(value))
}
/// Extract the inner value.
///
/// Once extracted, the value can of course drop again.
#[inline]
pub fn into_inner(mut self) -> T {
let inner = unsafe {
ptr::read(&mut *self)
};
// skip Drop, so we don't even have to overwrite
mem::forget(self);
inner
}
}
#[cfg(not(feature = "use_needs_drop"))]
#[inline]
fn needs_drop<T>() -> bool {
true
}
#[cfg(feature = "use_needs_drop")]
#[inline]
fn needs_drop<T>() -> bool {
unsafe {
::std::mem::needs_drop::<T>()
}
}
impl<T> Drop for NoDrop<T> {
fn drop(&mut self) {
if needs_drop::<T>() {
// inhibit drop
unsafe {
ptr::write(&mut self.0, Flag::Dropped(0));
}
}
}
}
impl<T> Deref for NoDrop<T> {
type Target = T;
// Use type invariant, always Flag::Alive.
#[inline]
fn deref(&self) -> &T {
match self.0 {
Flag::Alive(ref inner) => inner,
_ => unsafe { debug_assert_unreachable() }
}
}
}
impl<T> DerefMut for NoDrop<T> {
// Use type invariant, always Flag::Alive.
#[inline]
fn deref_mut(&mut self) -> &mut T {
match self.0 {
Flag::Alive(ref mut inner) => inner,
_ => unsafe { debug_assert_unreachable() }
}
}
}
#[cfg(test)]
#[test]
fn test_no_nonnullable_opt() {
// Make sure `Flag` does not apply the non-nullable pointer optimization
// as Option would do.
assert!(mem::size_of::<Flag<&i32>>() > mem::size_of::<&i32>());
assert!(mem::size_of::<Flag<Vec<i32>>>() > mem::size_of::<Vec<i32>>());
}
// copying this code saves us microcrate deps
#[inline]
unsafe fn debug_assert_unreachable() -> ! {
debug_assert!(false, "Reached unreachable section: this is a bug!");
enum Void { }
match *(1 as *const Void) { }
}
}
#[cfg(test)]
mod tests {
use super::NoDrop;
#[test]
fn test_drop() {
use std::cell::Cell;
let flag = &Cell::new(0);
struct Bump<'a>(&'a Cell<i32>);
impl<'a> Drop for Bump<'a> {
fn drop(&mut self) {
let n = self.0.get();
self.0.set(n + 1);
}
}
{
let _ = NoDrop::new([Bump(flag), Bump(flag)]);
}
assert_eq!(flag.get(), 0);
// test something with the nullable pointer optimization
flag.set(0);
{
let mut array = NoDrop::new(Vec::new());
array.push(vec![Bump(flag)]);
array.push(vec![Bump(flag), Bump(flag)]);
array.push(vec![]);
array.push(vec![Bump(flag)]);
drop(array.pop());
assert_eq!(flag.get(), 1);
drop(array.pop());
assert_eq!(flag.get(), 1);
drop(array.pop());
assert_eq!(flag.get(), 3);
}
// last one didn't drop.
assert_eq!(flag.get(), 3);
flag.set(0);
{
let array = NoDrop::new(Bump(flag));
array.into_inner();
assert_eq!(flag.get(), 1);
}
assert_eq!(flag.get(), 1);
}
}