From 3144b61ccc2ec024c19ccb41ff6964a86203ee14 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 16 Oct 2017 02:00:32 +0200 Subject: [PATCH] feat(lib): Implement Drop trait for Queue Implements the Drop trait to take care of closing the queue descriptor when a Queue instance is dropped. --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 475b2c0f4..cc3939d94 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ use std::ffi::CString; use std::fs::File; use std::io::Read; use std::string::ToString; +use std::ops::Drop; mod error; @@ -220,6 +221,15 @@ impl Queue { } } +impl Drop for Queue { + fn drop(&mut self) { + // Attempt to close the queue descriptor and discard any possible errors. + // The only error thrown in the C-code is EINVAL, which would mean that the + // descriptor has already been closed. + mqueue::mq_close(self.queue_descriptor).ok(); + } +} + // Creates the default queue mode (0600). fn default_mode() -> stat::Mode { let mut mode = stat::Mode::empty();