drm: Replace inner Option on DrmNode and use mem::forget

This commit is contained in:
i509VCB 2022-01-31 13:48:13 -06:00
parent d04a999bf8
commit 0c8cb49129
1 changed files with 9 additions and 14 deletions

View File

@ -7,7 +7,7 @@ use libc::dev_t;
use std::{ use std::{
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
fs, io, fs, io, mem,
os::unix::prelude::{AsRawFd, IntoRawFd, RawFd}, os::unix::prelude::{AsRawFd, IntoRawFd, RawFd},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -20,8 +20,7 @@ use nix::{
/// A node which refers to a DRM device. /// A node which refers to a DRM device.
#[derive(Debug)] #[derive(Debug)]
pub struct DrmNode { pub struct DrmNode {
// Always `Some`, None variant is used when taking ownership fd: RawFd,
fd: Option<RawFd>,
dev: dev_t, dev: dev_t,
ty: NodeType, ty: NodeType,
} }
@ -56,11 +55,7 @@ impl DrmNode {
_ => return Err(CreateDrmNodeError::NotDrmNode), _ => return Err(CreateDrmNodeError::NotDrmNode),
}; };
Ok(DrmNode { Ok(DrmNode { fd, dev, ty })
fd: Some(fd),
dev,
ty,
})
} }
/// Returns the type of the DRM node. /// Returns the type of the DRM node.
@ -120,22 +115,22 @@ impl Display for DrmNode {
} }
impl IntoRawFd for DrmNode { impl IntoRawFd for DrmNode {
fn into_raw_fd(mut self) -> RawFd { fn into_raw_fd(self) -> RawFd {
self.fd.take().unwrap() let fd = self.fd;
mem::forget(self);
fd
} }
} }
impl AsRawFd for DrmNode { impl AsRawFd for DrmNode {
fn as_raw_fd(&self) -> RawFd { fn as_raw_fd(&self) -> RawFd {
self.fd.unwrap() self.fd
} }
} }
impl Drop for DrmNode { impl Drop for DrmNode {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(fd) = self.fd { let _ = close(self.fd);
let _ = close(fd);
}
} }
} }