1.0.0[][src]Trait nom::lib::std::ops::Drop

#[lang = "drop"]pub trait Drop {
    fn drop(&mut self);
}
[]

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:

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 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

fn drop(&mut self)[]

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.

Implementations on Foreign Types

impl<'_, T> Drop for MutexGuard<'_, T> where
    T: ?Sized
[src][]

impl<T> Drop for RwLock<T> where
    T: ?Sized
[src][]

impl<W> Drop for BufWriter<W> where
    W: Write
[src][]

impl<T> Drop for Receiver<T>[src][]

impl<'_, T> Drop for RwLockWriteGuard<'_, T> where
    T: ?Sized
[src][]

impl Drop for CString[src][]

impl<T> Drop for Sender<T>[src][]

impl<'_, T> Drop for RwLockReadGuard<'_, T> where
    T: ?Sized
[src][]

impl<T> Drop for SyncSender<T>[src][]

impl<T> Drop for SyncOnceCell<T>[src][]

impl<'f> Drop for VaListImpl<'f>[src][]

impl Drop for Waker[src][]

impl<T, const N: usize> Drop for IntoIter<T, N>[src][]

impl<T> Drop for Rc<T> where
    T: ?Sized
[src][]

fn drop(&mut self)[src][]

Drops the Rc.

This will decrement the strong reference count. If the strong reference count reaches zero then the only other references (if any) are Weak, so we drop the inner value.

Examples

use std::rc::Rc;

struct Foo;

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

let foo  = Rc::new(Foo);
let foo2 = Rc::clone(&foo);

drop(foo);    // Doesn't print anything
drop(foo2);   // Prints "dropped!"

impl<T> Drop for Weak<T> where
    T: ?Sized
[src][]

fn drop(&mut self)[src][]

Drops the Weak pointer.

Examples

use std::rc::{Rc, Weak};

struct Foo;

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

let foo = Rc::new(Foo);
let weak_foo = Rc::downgrade(&foo);
let other_weak_foo = Weak::clone(&weak_foo);

drop(weak_foo);   // Doesn't print anything
drop(foo);        // Prints "dropped!"

assert!(other_weak_foo.upgrade().is_none());

impl<T> Drop for Arc<T> where
    T: ?Sized
[src][]

fn drop(&mut self)[src][]

Drops the Arc.

This will decrement the strong reference count. If the strong reference count reaches zero then the only other references (if any) are Weak, so we drop the inner value.

Examples

use std::sync::Arc;

struct Foo;

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

let foo  = Arc::new(Foo);
let foo2 = Arc::clone(&foo);

drop(foo);    // Doesn't print anything
drop(foo2);   // Prints "dropped!"

impl<T> Drop for Weak<T> where
    T: ?Sized
[src][]

fn drop(&mut self)[src][]

Drops the Weak pointer.

Examples

use std::sync::{Arc, Weak};

struct Foo;

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

let foo = Arc::new(Foo);
let weak_foo = Arc::downgrade(&foo);
let other_weak_foo = Weak::clone(&weak_foo);

drop(weak_foo);   // Doesn't print anything
drop(foo);        // Prints "dropped!"

assert!(other_weak_foo.upgrade().is_none());

Implementors

impl<'_> Drop for nom::lib::std::string::Drain<'_>[src][+]

impl<'_, I> Drop for Splice<'_, I> where
    I: Iterator
[src][+]

impl<'_, K, V, F> Drop for nom::lib::std::collections::btree_map::DrainFilter<'_, K, V, F> where
    F: FnMut(&K, &mut V) -> bool
[src][+]

impl<'_, T> Drop for PeekMut<'_, T> where
    T: Ord
[src][+]

impl<'_, T> Drop for nom::lib::std::collections::vec_deque::Drain<'_, T>[src][+]

impl<'_, T> Drop for nom::lib::std::vec::Drain<'_, T>[src][+]

impl<'_, T, F> Drop for nom::lib::std::collections::btree_set::DrainFilter<'_, T, F> where
    F: FnMut(&T) -> bool
[src][+]

impl<'_, T, F> Drop for nom::lib::std::collections::linked_list::DrainFilter<'_, T, F> where
    F: FnMut(&mut T) -> bool
[src][+]

impl<'_, T, F> Drop for nom::lib::std::vec::DrainFilter<'_, T, F> where
    F: FnMut(&mut T) -> bool
[src][+]

impl<'a, T> Drop for DrainSorted<'a, T> where
    T: Ord
[src][+]

fn drop(&mut self)[src][]

Removes heap elements in heap order.

impl<K, V> Drop for nom::lib::std::collections::btree_map::IntoIter<K, V>[src][+]

impl<K, V> Drop for BTreeMap<K, V>[src][+]

impl<T> Drop for LinkedList<T>[src][+]

impl<T> Drop for VecDeque<T>[src][+]

impl<T> Drop for Box<T> where
    T: ?Sized
[src][+]

impl<T> Drop for Vec<T>[src][+]

impl<T> Drop for nom::lib::std::vec::IntoIter<T>[src][+]

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

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

impl<'a, A: Array> Drop for Drain<'a, A> where
    A::Item: 'a, 

impl<P> Drop for Atom<P> where
    P: IntoRawPtr + FromRawPtr, 

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

impl<T> Drop for Sender<T>

impl Drop for PingSource

impl Drop for Signals

impl<'a> Drop for UiCell<'a>

impl<T> Drop for BoxAllocation<T>

impl<T> Drop for Sender<T>

impl<T> Drop for Receiver<T>

impl<'a> Drop for SelectedOperation<'a>

impl<T> Drop for Injector<T>

impl<T> 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<'a, T: ?Sized> Drop for ShardedLockWriteGuard<'a, T>

impl Drop for WaitGroup

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 Enter

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

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

impl<Fut> Drop for FuturesUnordered<Fut>

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 Drop for RawInstance

impl<'a, R: Resources> Drop for AccessGuard<'a, R>

impl<B: Backend> Drop for DescriptorAllocator<B>

impl Drop for DedicatedAllocator

impl<B: Backend> Drop for GeneralAllocator<B>

impl<B: Backend> Drop for LinearAllocator<B>

impl<B: Backend> Drop for Heaps<B>

impl<'a, 'b, T, B: Backend> Drop for Writer<'a, 'b, T, B>

impl<T: ?Sized> Drop for Buffer<T> where
    T: Content, 

impl Drop for BufferAny

impl Drop for RenderBufferAny

impl<'a> Drop for TransformFeedbackSession<'a>

impl Drop for ResidentTexture

impl<T> Drop for BufferTexture<T> where
    [T]: BufferContent, 

impl Drop for TextureAny

impl Drop for Context

impl Drop for SyncFence

impl Drop for LinearSyncFence

impl Drop for Frame

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

impl Drop for Library

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

impl<K, V> Drop for IntoIter<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 Drop for Registration

impl Drop for Dir

impl<'d> Drop for Iter<'d>

impl Drop for InterfaceAddressIterator

impl Drop for PtyMaster

impl<'a> Drop for AioCb<'a>

impl Drop for SignalFd

impl Drop for Relevant

impl<'a, B> Drop for RenderPassInlineEncoder<'a, B> where
    B: Backend, 

impl<'a, B> Drop for RenderPassSecondaryEncoder<'a, B> where
    B: Backend, 

impl<B> Drop for Factory<B> where
    B: Backend, 

impl Drop for DedicatedAllocator

impl<T> Drop for Escape<T>

impl<T> Drop for Terminal<T>

impl<B: Backend> Drop for ShaderStorage<B>

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

impl Drop for Compiler

impl<'a> Drop for CompileOptions<'a>

impl Drop for CompilationArtifact

impl Drop for DynamicLibrary

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

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

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

impl Drop for DataDevice

impl Drop for DataOffer

impl Drop for MemPool

impl Drop for BasicFrame

impl Drop for ConceptFrame

impl<F: Frame> Drop for Window<F>

impl Drop for ThreadedClipboard

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

impl Drop for Span

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

impl Drop for DefaultGuard

impl<T, A> Drop for CpuBufferPoolChunk<T, A> where
    A: MemoryPool, 

impl Drop for UnsafeBuffer

impl<F, B> Drop for BufferView<F, B> where
    B: BufferAccess, 

impl Drop for StandardCommandPoolAlloc

impl Drop for UnsafeCommandPool

impl<F, Cb> Drop for CommandBufferExecFuture<F, Cb> where
    F: GpuFuture,
    Cb: CommandBuffer, 

impl Drop for StdDescriptorPoolAlloc

impl Drop for UnsafeDescriptorPool

impl Drop for UnsafeDescriptorSetLayout

impl<L> Drop for PipelineLayout<L>

impl Drop for Device

impl<Rp, A> Drop for Framebuffer<Rp, A>

impl<D> Drop for RenderPass<D>

impl Drop for UnsafeImage

impl Drop for UnsafeImageView

impl Drop for DebugCallback

impl Drop for Instance

impl Drop for DeviceMemory

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

impl Drop for StdHostVisibleMemoryTypePoolAlloc

impl Drop for StdNonHostVisibleMemoryTypePoolAlloc

impl Drop for PipelineCache

impl Drop for ShaderModule

impl Drop for UnsafeQueryPool

impl Drop for Sampler

impl<W> Drop for Surface<W>

impl<W> Drop for Swapchain<W>

impl<W> Drop for SwapchainAcquireFuture<W>

impl<P, W> Drop for PresentFuture<P, W> where
    P: GpuFuture, 

impl Drop for Event

impl<D> Drop for Fence<D> where
    D: SafeDeref<Target = Device>, 

impl<F> Drop for FenceSignalFuture<F> where
    F: GpuFuture, 

impl<F> Drop for SemaphoreSignalFuture<F> where
    F: GpuFuture, 

impl<D> Drop for Semaphore<D> where
    D: SafeDeref<Target = Device>, 

impl Drop for ReadEventsGuard

impl Drop for CursorTheme

impl Drop for Socket

impl Drop for UserData

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

impl Drop for WlEglSurface

impl Drop for Sampler

impl Drop for BindGroupLayout

impl Drop for BindGroup

impl Drop for ShaderModule

impl Drop for PipelineLayout

impl Drop for RenderPipeline

impl Drop for ComputePipeline

impl Drop for CommandBuffer

impl Drop for RenderBundle

impl Drop for Device

impl<'_> Drop for BufferView<'_>

impl<'_> Drop for BufferViewMut<'_>

impl Drop for Buffer

impl Drop for Texture

impl Drop for TextureView

impl<'a> Drop for RenderPass<'a>

impl<'a> Drop for ComputePass<'a>

impl Drop for SwapChainTexture

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

impl<G: GlobalIdentityHandlerFactory> Drop for Global<G>

impl Drop for Window

impl<T> Drop for Event<T>

impl<T> Drop for Error<T>

impl<'a, T: Copy + CookieSeq> Drop for Cookie<'a, T>

impl<T> Drop for Reply<T>

impl Drop for Connection