summary refs log tree commit diff
path: root/msg_socket2/src/fd.rs
blob: 648be3e7ab81d4b4d503d32fc8fae8de9b0d54d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright 2020, Alyssa Ross

use libc::close;
use std::mem::forget;
use std::os::unix::prelude::*;

/// A type representing ownership of a file descriptor.
pub struct Fd(RawFd);

impl Fd {
    /// Construct an owned `Fd` from a raw file descriptor.
    ///
    /// This function is unsafe because `Fd` must be the sole owner of
    /// the file descriptor, so that it can be closed when the `Fd` is
    /// dropped.
    pub unsafe fn new(fd: RawFd) -> Self {
        Self(fd)
    }

    /// Converts this `Fd` into a different implementation of
    /// `FromRawFd` containing the same file descriptor.
    ///
    /// No checks that the file descriptor is of the appropriate type
    /// are performed.  The file descriptor will remain open.
    pub fn specialize<T: FromRawFd>(self) -> T {
        let fd = self.into_raw_fd();

        // Safe because we transfer ownership of the file descriptor.
        unsafe { T::from_raw_fd(fd) }
    }
}

impl IntoRawFd for Fd {
    fn into_raw_fd(self) -> RawFd {
        let fd = self.0;
        forget(self);
        fd
    }
}

impl AsRawFd for Fd {
    fn as_raw_fd(&self) -> RawFd {
        self.0
    }
}

impl FromRawFd for Fd {
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Self(fd)
    }
}

impl Drop for Fd {
    fn drop(&mut self) {
        // Safe because we have sole ownership of fd.
        unsafe { close(self.0) };
    }
}