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