@@ -42,40 +42,7 @@ impl Kvm {
42
42
// Open `/dev/kvm` using `O_CLOEXEC` flag.
43
43
let fd = Self :: open_with_cloexec ( true ) ?;
44
44
// 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) } )
79
46
}
80
47
81
48
/// Opens `/dev/kvm` and returns the fd number on success.
@@ -93,10 +60,11 @@ impl Kvm {
93
60
///
94
61
/// ```
95
62
/// # use kvm_ioctls::Kvm;
63
+ /// # use std::os::unix::io::FromRawFd;
96
64
/// let kvm_fd = Kvm::open_with_cloexec(false).unwrap();
97
65
/// // 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) };
100
68
/// ```
101
69
///
102
70
pub fn open_with_cloexec ( close_on_exec : bool ) -> Result < RawFd > {
@@ -487,6 +455,34 @@ impl AsRawFd for Kvm {
487
455
}
488
456
489
457
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
+ ///
490
486
unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
491
487
Kvm {
492
488
kvm : File :: from_raw_fd ( fd) ,
0 commit comments