Skip to content

Commit aed8f65

Browse files
smortimealxiord
authored andcommitted
Remove Kvm::new_with_fd_number function
* replace new_with_fd_number with from_raw_fd * move documentation to from_raw_fd * update doc tests Fixes: #115 Signed-off-by: Schuyler Mortimer <[email protected]>
1 parent a638005 commit aed8f65

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/ioctls/system.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,7 @@ impl Kvm {
4242
// Open `/dev/kvm` using `O_CLOEXEC` flag.
4343
let fd = Self::open_with_cloexec(true)?;
4444
// Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
45-
Ok(unsafe { Self::new_with_fd_number(fd) })
46-
}
47-
48-
/// Creates a new Kvm object assuming `fd` represents an existing open file descriptor
49-
/// associated with `/dev/kvm`.
50-
///
51-
/// For usage examples check [open_with_cloexec()](struct.Kvm.html#method.open_with_cloexec).
52-
///
53-
/// # Arguments
54-
///
55-
/// * `fd` - File descriptor for `/dev/kvm`.
56-
///
57-
/// # Safety
58-
///
59-
/// This function is unsafe as the primitives currently returned have the contract that
60-
/// they are the sole owner of the file descriptor they are wrapping. Usage of this function
61-
/// could accidentally allow violating this contract which can cause memory unsafety in code
62-
/// that relies on it being true.
63-
///
64-
/// The caller of this method must make sure the fd is valid and nothing else uses it.
65-
///
66-
/// # Example
67-
///
68-
/// ```
69-
/// # use kvm_ioctls::Kvm;
70-
/// let kvm_fd = Kvm::open_with_cloexec(true).unwrap();
71-
/// // Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
72-
/// let kvm = unsafe { Kvm::new_with_fd_number(kvm_fd) };
73-
/// ```
74-
///
75-
pub unsafe fn new_with_fd_number(fd: RawFd) -> Self {
76-
Kvm {
77-
kvm: File::from_raw_fd(fd),
78-
}
45+
Ok(unsafe { Self::from_raw_fd(fd) })
7946
}
8047

8148
/// Opens `/dev/kvm` and returns the fd number on success.
@@ -93,10 +60,11 @@ impl Kvm {
9360
///
9461
/// ```
9562
/// # use kvm_ioctls::Kvm;
63+
/// # use std::os::unix::io::FromRawFd;
9664
/// let kvm_fd = Kvm::open_with_cloexec(false).unwrap();
9765
/// // The `kvm_fd` can now be passed to another process where we can use
98-
/// // `new_with_fd_number` for creating a `Kvm` object:
99-
/// let kvm = unsafe { Kvm::new_with_fd_number(kvm_fd) };
66+
/// // `from_raw_fd` for creating a `Kvm` object:
67+
/// let kvm = unsafe { Kvm::from_raw_fd(kvm_fd) };
10068
/// ```
10169
///
10270
pub fn open_with_cloexec(close_on_exec: bool) -> Result<RawFd> {
@@ -487,6 +455,34 @@ impl AsRawFd for Kvm {
487455
}
488456

489457
impl FromRawFd for Kvm {
458+
/// Creates a new Kvm object assuming `fd` represents an existing open file descriptor
459+
/// associated with `/dev/kvm`.
460+
///
461+
/// For usage examples check [open_with_cloexec()](struct.Kvm.html#method.open_with_cloexec).
462+
///
463+
/// # Arguments
464+
///
465+
/// * `fd` - File descriptor for `/dev/kvm`.
466+
///
467+
/// # Safety
468+
///
469+
/// This function is unsafe as the primitives currently returned have the contract that
470+
/// they are the sole owner of the file descriptor they are wrapping. Usage of this function
471+
/// could accidentally allow violating this contract which can cause memory unsafety in code
472+
/// that relies on it being true.
473+
///
474+
/// The caller of this method must make sure the fd is valid and nothing else uses it.
475+
///
476+
/// # Example
477+
///
478+
/// ```
479+
/// # use kvm_ioctls::Kvm;
480+
/// # use std::os::unix::io::FromRawFd;
481+
/// let kvm_fd = Kvm::open_with_cloexec(true).unwrap();
482+
/// // Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
483+
/// let kvm = unsafe { Kvm::from_raw_fd(kvm_fd) };
484+
/// ```
485+
///
490486
unsafe fn from_raw_fd(fd: RawFd) -> Self {
491487
Kvm {
492488
kvm: File::from_raw_fd(fd),

0 commit comments

Comments
 (0)