Trait scale_info::prelude::ops::Drop

1.0.0 · source ·
pub trait Drop {
    fn drop(&mut self);
}
Expand description

Custom code within the destructor.

When a value is no longer needed, Rust will run a “destructor” on that value. The most common way that a value is no longer needed is when it goes out of scope. Destructors may still run in other circumstances, but we’re going to focus on scope for the examples here. To learn about some of those other cases, please see the reference section on destructors.

This destructor consists of two components:

  • A call to Drop::drop for that value, if this special Drop trait is implemented for its type.
  • The automatically generated “drop glue” which recursively calls the destructors of all the fields of this value.

As Rust automatically calls the destructors of all contained fields, you don’t have to implement Drop in most cases. But there are some cases where it is useful, for example for types which directly manage a resource. That resource may be memory, it may be a file descriptor, it may be a network socket. Once a value of that type is no longer going to be used, it should “clean up” its resource by freeing the memory or closing the file or socket. This is the job of a destructor, and therefore the job of Drop::drop.

Examples

To see destructors in action, let’s take a look at the following program:

struct HasDrop;

impl Drop for HasDrop {
    fn drop(&mut self) {
        println!("Dropping HasDrop!");
    }
}

struct HasTwoDrops {
    one: HasDrop,
    two: HasDrop,
}

impl Drop for HasTwoDrops {
    fn drop(&mut self) {
        println!("Dropping HasTwoDrops!");
    }
}

fn main() {
    let _x = HasTwoDrops { one: HasDrop, two: HasDrop };
    println!("Running!");
}

Rust will first call Drop::drop for _x and then for both _x.one and _x.two, meaning that running this will print

Running!
Dropping HasTwoDrops!
Dropping HasDrop!
Dropping HasDrop!

Even if we remove the implementation of Drop for HasTwoDrop, the destructors of its fields are still called. This would result in

Running!
Dropping HasDrop!
Dropping HasDrop!

You cannot call Drop::drop yourself

Because Drop::drop is used to clean up a value, it may be dangerous to use this value after the method has been called. As Drop::drop does not take ownership of its input, Rust prevents misuse by not allowing you to call Drop::drop directly.

In other words, if you tried to explicitly call Drop::drop in the above example, you’d get a compiler error.

If you’d like to explicitly call the destructor of a value, mem::drop can be used instead.

Drop order

Which of our two HasDrop drops first, though? For structs, it’s the same order that they’re declared: first one, then two. If you’d like to try this yourself, you can modify HasDrop above to contain some data, like an integer, and then use it in the println! inside of Drop. This behavior is guaranteed by the language.

Unlike for structs, local variables are dropped in reverse order:

struct Foo;

impl Drop for Foo {
    fn drop(&mut self) {
        println!("Dropping Foo!")
    }
}

struct Bar;

impl Drop for Bar {
    fn drop(&mut self) {
        println!("Dropping Bar!")
    }
}

fn main() {
    let _foo = Foo;
    let _bar = Bar;
}

This will print

Dropping Bar!
Dropping Foo!

Please see the reference for the full rules.

Copy and Drop are exclusive

You cannot implement both Copy and Drop on the same type. Types that are Copy get implicitly duplicated by the compiler, making it very hard to predict when, and how often destructors will be executed. As such, these types cannot have destructors.

Required Methods§

Executes the destructor for this type.

This method is called implicitly when the value goes out of scope, and cannot be called explicitly (this is compiler error E0040). However, the mem::drop function in the prelude can be used to call the argument’s Drop implementation.

When this method has been called, self has not yet been deallocated. That only happens after the method is over. If this wasn’t the case, self would be a dangling reference.

Panics

Given that a panic! will call drop as it unwinds, any panic! in a drop implementation will likely abort.

Note that even if this panics, the value is considered to be dropped; you must not cause drop to be called again. This is normally automatically handled by the compiler, but when using unsafe code, can sometimes occur unintentionally, particularly when using ptr::drop_in_place.

Implementors§

impl Drop for Error

impl<T> Drop for Sender<T>

impl<T> Drop for Receiver<T>

impl Drop for Executor<'_>

impl Drop for Timer

impl<T> Drop for Async<T>

impl<T: ?Sized> Drop for MutexGuard<'_, T>

impl<T: ?Sized> Drop for MutexGuardArc<T>

impl<T> Drop for OnceCell<T>

impl<T: ?Sized> Drop for RwLockReadGuard<'_, T>

impl Drop for SemaphoreGuard<'_>

impl<T> Drop for JoinHandle<T>

impl Drop for File

impl Drop for Runnable

impl<T> Drop for Task<T>

impl Drop for BacktraceFrameFmt<'_, '_, '_>

impl<W: Write> Drop for EncoderWriter<W>

impl<T, U> Drop for Cow<'_, T, U>where
    T: Beef + ?Sized,
    U: Capacity,

impl Drop for Seed

impl Drop for Bump

impl<C, N> Drop for ChaChaPoly1305<C, N>where
    C: NewCipher<KeySize = U32, NonceSize = N> + StreamCipher + StreamCipherSeek,
    N: ArrayLength<u8>,

impl Drop for TimingToken

impl<T> Drop for Sender<T>

impl<T> Drop for Receiver<T>

impl Drop for SelectedOperation<'_>

impl<T> Drop for Injector<T>

impl<T: ?Sized + Pointable> Drop for Owned<T>

impl Drop for LocalHandle

impl Drop for Guard

impl<T> Drop for ArrayQueue<T>

impl<T> Drop for SegQueue<T>

impl<T> Drop for AtomicCell<T>

impl<T: ?Sized> Drop for ShardedLockWriteGuard<'_, T>

impl Drop for WaitGroup

impl<AccountId, FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>, Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>, ConcreteAssets: Mutate<AccountId> + Transfer<AccountId> + Balanced<AccountId>, HandleRefund: TakeRevenue> Drop for TakeFirstAssetTrader<AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund>

impl<C> Drop for SigningKey<C>where
    C: PrimeCurve + ProjectiveArithmetic,
    Scalar<C>: Invert<Output = Scalar<C>> + Reduce<C::UInt> + SignPrimitive<C>,
    SignatureSize<C>: ArrayLength<u8>,

impl Drop for SecretKey

impl<C> Drop for SecretKey<C>where
    C: Curve,

impl Drop for Event

impl<W: Write> Drop for GzEncoder<W>

impl<B: Balance, OnDrop: HandleImbalanceDrop<B>, OppositeOnDrop: HandleImbalanceDrop<B>> Drop for Imbalance<B, OnDrop, OppositeOnDrop>

impl<A: AssetId, B: Balance, OnDrop: HandleImbalanceDrop<A, B>, OppositeOnDrop: HandleImbalanceDrop<A, B>> Drop for Imbalance<A, B, OnDrop, OppositeOnDrop>

impl<F: FilterStack<T>, T> Drop for FilterStackGuard<F, T>

impl<F: FilterStack<T>, T> Drop for ClearFilterGuard<F, T>

impl<T> Drop for Receiver<T>

impl<T> Drop for UnboundedReceiver<T>

impl<T> Drop for Sender<T>

impl<T> Drop for Receiver<T>

impl Drop for ThreadPool

impl Drop for Enter

impl<T> Drop for LocalFutureObj<'_, T>

impl Drop for Delay

impl<Fut> Drop for Shared<Fut>where
    Fut: Future,

impl<Fut> Drop for FuturesUnordered<Fut>

impl<T> Drop for BiLockGuard<'_, T>

impl<T: ?Sized> Drop for OwnedMutexLockFuture<T>

impl<T: ?Sized> Drop for OwnedMutexGuard<T>

impl<T: ?Sized> Drop for MutexLockFuture<'_, T>

impl<T: ?Sized> Drop for MutexGuard<'_, T>

impl<T: ?Sized, U: ?Sized> Drop for MappedMutexGuard<'_, T, U>

impl<T, N> Drop for GenericArrayIter<T, N>where
    N: ArrayLength<T>,

impl Drop for RecvStream

impl<T, A: Allocator + Clone> Drop for RawTable<T, A>

impl<T, A: Allocator + Clone> Drop for RawIntoIter<T, A>

impl<T, A: Allocator + Clone> Drop for RawDrain<'_, T, A>

impl<'a, K, V, F, A> Drop for DrainFilter<'a, K, V, F, A>where
    F: FnMut(&K, &mut V) -> bool,
    A: Allocator + Clone,

impl<'a, K, F, A: Allocator + Clone> Drop for DrainFilter<'a, K, F, A>where
    F: FnMut(&K) -> bool,

impl<'a, T> Drop for Drain<'a, T>

impl<T> Drop for IntoIter<T>

impl<'a, T> Drop for ValueDrain<'a, T>

impl<AssetId, Balance: FixedPointOperand + TryInto<u128>, Price: FixedPointNumber, ConvertWeightToFee: WeightToFee<Balance = Balance>, AcceptedCurrencyPrices: NativePriceOracle<AssetId, Price>, ConvertCurrency: Convert<MultiAsset, Option<AssetId>>, Revenue: TakeRevenue> Drop for MultiCurrencyTrader<AssetId, Balance, Price, ConvertWeightToFee, AcceptedCurrencyPrices, ConvertCurrency, Revenue>

impl Drop for GaiFuture

impl Drop for OwnedFd

impl<Target: FilelikeViewType> Drop for FilelikeView<'_, Target>

impl<Target: SocketlikeViewType> Drop for SocketlikeView<'_, Target>

impl<'a, K, I, F> Drop for Group<'a, K, I, F>where
    I: Iterator,
    I::Item: 'a,

impl<'a, I> Drop for Chunk<'a, I>where
    I: Iterator,
    I::Item: 'a,

impl<'a> Drop for MethodResourcesBuilder<'a>

impl Drop for Client

impl<Notif> Drop for Subscription<Notif>

impl Drop for SigningKey

impl<T> Drop for Chan<T>

impl<C> Drop for Substream<C>where
    C: AsyncRead + AsyncWrite + Unpin,

impl<T: Zeroize> Drop for SecretKey<T>

impl<T> Drop for TcpListenStream<T>where
    T: Provider,

impl Drop for Connection

impl<K, V, S> Drop for LinkedHashMap<K, V, S>

impl<K, V> Drop for IntoIter<K, V>

impl<'a, K, V> Drop for Drain<'a, K, V>

impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Drop for MutexGuard<'a, R, T>

impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Drop for MappedMutexGuard<'a, R, T>

impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> Drop for ReentrantMutexGuard<'a, R, G, T>

impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> Drop for MappedReentrantMutexGuard<'a, R, G, T>

impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for RwLockReadGuard<'a, R, T>

impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for RwLockWriteGuard<'a, R, T>

impl<'a, R: RawRwLockUpgrade + 'a, T: ?Sized + 'a> Drop for RwLockUpgradableReadGuard<'a, R, T>

impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for MappedRwLockReadGuard<'a, R, T>

impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for MappedRwLockWriteGuard<'a, R, T>

impl<K, V, S> Drop for LruCache<K, V, S>

impl Drop for Span

impl<'a> Drop for Log<'a>

impl Drop for Socket

impl Drop for SignalFd

impl<T> Drop for OnceBox<T>

impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Drop for PositiveImbalance<T, GetCurrencyId>

impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Drop for NegativeImbalance<T, GetCurrencyId>

impl<T: Config<I>, I: 'static> Drop for DustCleaner<T, I>

impl<T: Config<I>, I: 'static> Drop for PositiveImbalance<T, I>

impl<T: Config<I>, I: 'static> Drop for NegativeImbalance<T, I>

impl Drop for Db

impl<T> Drop for SendWrapper<T>

impl<'a, T: Ord + Send> Drop for Drain<'a, T>

impl<'a, T: Send> Drop for Drain<'a, T>

impl<'a> Drop for Drain<'a>

impl<'data, T: Send> Drop for Drain<'data, T>

impl Drop for ThreadPool

impl Drop for Ast

impl Drop for ClassSet

impl Drop for Hir

impl Drop for LockGuard

impl<'db> Drop for Checkpoint<'db>

impl<'a> Drop for BoundColumnFamily<'a>

impl<'a, D: DBAccess> Drop for DBRawIteratorWithThreadMode<'a, D>

impl Drop for Options

impl Drop for ReadOptions

impl Drop for DBPath

impl<'a> Drop for DBPinnableSlice<'a>

impl Drop for PerfContext

impl<'a, D: DBAccess> Drop for SnapshotWithThreadMode<'a, D>

impl<'a> Drop for SstFileWriter<'a>

impl Drop for WriteBatch

impl Drop for Dir

impl Drop for OwnedFd

impl<B: BlockT> Drop for RefTrackingState<B>

impl<B: BlockT, Transaction> Drop for BasicQueue<B, Transaction>

impl<'a, T> Drop for SharedDataLocked<'a, T>

impl<M, R> Drop for Receiver<M, R>where
    R: Unsubscribe,

impl<'a, T> Drop for ReadySinkEvent<'a, T>

impl Drop for SecretKey

impl Drop for Keypair

impl<T, F, S> Drop for ScopeGuard<T, F, S>where
    F: FnOnce(T),
    S: Strategy,

impl<C: Context> Drop for Secp256k1<C>

impl<S> Drop for Secret<S>where
    S: Zeroize,

impl<'a, T, C> Drop for Ref<'a, T, C>where
    T: Clear + Default,
    C: Config,

impl<'a, T, C> Drop for RefMut<'a, T, C>where
    T: Clear + Default,
    C: Config,

impl<T, C> Drop for OwnedRef<T, C>where
    T: Clear + Default,
    C: Config,

impl<T, C> Drop for OwnedRefMut<T, C>where
    T: Clear + Default,
    C: Config,

impl<'a, T, C: Config> Drop for Entry<'a, T, C>

impl<T, C> Drop for OwnedEntry<T, C>where
    C: Config,

impl<'a, K: Key, V> Drop for Drain<'a, K, V>

impl<'a, K: Key, V> Drop for Drain<'a, K, V>

impl<'a, K: Key, V> Drop for Drain<'a, K, V>

impl<'a, K: Key, V> Drop for Drain<'a, K, V>

impl<'a, K: Key, V> Drop for Drain<'a, K, V>

impl<'a, T: 'a + Array> Drop for Drain<'a, T>

impl<A: Array> Drop for SmallVec<A>

impl<A: Array> Drop for IntoIter<A>

impl<W: Write> Drop for FrameEncoder<W>

impl<F: FnOnce()> Drop for DeferGuard<F>

impl Drop for Pair

impl Drop for AbortGuard

impl<'a, 'b, L: Lockable> Drop for StorageLockGuard<'a, 'b, L>

impl<'a, B, H, Exec> Drop for StateMachine<'a, B, H, Exec>where
    H: Hasher,
    B: Backend<H>,

impl<H: Hasher> Drop for LocalTrieCache<H>

impl<'a, T: ?Sized> Drop for MutexGuard<'a, T>

impl<'rwlock, T: ?Sized> Drop for RwLockReadGuard<'rwlock, T>

impl<'rwlock, T: ?Sized> Drop for RwLockUpgradeableGuard<'rwlock, T>

impl<'rwlock, T: ?Sized> Drop for RwLockWriteGuard<'rwlock, T>

impl Drop for TokenBuffer

impl<'a> Drop for ParseBuffer<'a>

impl Drop for TempDir

impl Drop for TempPath

impl<T: Send> Drop for ThreadLocal<T>

impl<'p, A: Array, I: Iterator<Item = A::Item>> Drop for ArrayVecSplice<'p, A, I>

impl<'p, 's, T: Default> Drop for SliceVecDrain<'p, 's, T>

impl<'p, A: Array, I: Iterator<Item = A::Item>> Drop for TinyVecSplice<'p, A, I>

impl<T: AsRawFd> Drop for AsyncFd<T>

impl Drop for AbortHandle

impl<T> Drop for JoinHandle<T>

impl Drop for Runtime

impl<T> Drop for Sender<T>

impl<T> Drop for Receiver<T>

impl<T> Drop for Permit<'_, T>

impl<T> Drop for OwnedPermit<T>

impl<T: ?Sized> Drop for MutexGuard<'_, T>

impl<T: ?Sized> Drop for OwnedMutexGuard<T>

impl<'a, T: ?Sized> Drop for MappedMutexGuard<'a, T>

impl Drop for Notified<'_>

impl<T> Drop for Sender<T>

impl<T> Drop for Receiver<T>

impl Drop for SemaphorePermit<'_>

impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockReadGuard<T, U>

impl<T: ?Sized> Drop for OwnedRwLockWriteGuard<T>

impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockMappedWriteGuard<T, U>

impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T>

impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T>

impl<'a, T: ?Sized> Drop for RwLockMappedWriteGuard<'a, T>

impl<T> Drop for OnceCell<T>

impl<T> Drop for Receiver<T>

impl<T> Drop for Sender<T>

impl Drop for LocalSet

impl<T: 'static, F> Drop for TaskLocalFuture<T, F>

impl<T> Drop for JoinSet<T>

impl Drop for DropGuard

impl Drop for Span

impl<'a> Drop for Entered<'a>

impl Drop for EnteredSpan

impl<'a, L> Drop for TrieDBMut<'a, L>where
    L: TrieLayout,

impl<'a, T> Drop for Locked<'a, T>

impl<'a> Drop for PathSegmentsMut<'a>

impl<'a> Drop for UrlQuery<'a>

impl Drop for Taker

impl<T> Drop for Closure<T>where
    T: ?Sized,

impl Drop for JsValue

impl Drop for Delay

impl Drop for Timer

impl<T> Drop for Store<T>

impl Drop for CodeMemory

impl Drop for JitDumpFile

impl Drop for VMExternRef

impl Drop for Mmap

impl Drop for Table

impl<T: Get<(AssetId, u128)>, R: TakeRevenue> Drop for FixedRateOfFungible<T, R>

impl<WeightToFee: WeightToFeeT<Balance = Currency::Balance>, AssetId: Get<MultiLocation>, AccountId, Currency: CurrencyT<AccountId>, OnUnbalanced: OnUnbalancedT<Currency::NegativeImbalance>> Drop for UsingComponents<WeightToFee, AssetId, AccountId, Currency, OnUnbalanced>

impl<T> Drop for Connection<T>

impl<Z> Drop for Zeroizing<Z>where
    Z: Zeroize,

impl<W, F> Drop for AutoFlushDecoder<'_, W, F>where
    W: Write,
    F: FnMut(Result<()>),

impl<W: Write, F: FnMut(Result<W>)> Drop for AutoFinishEncoder<'_, W, F>

impl<'a> Drop for CCtx<'a>

impl Drop for DCtx<'_>

impl<'a> Drop for CDict<'a>

impl<'a> Drop for DDict<'a>