Skip to content

Commit 9016fef

Browse files
committed
rust: to_result only needs the return code
There is no need for it to take a closure as argument as it just calls it. Removing the closure simplifies the callers. Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 1ae6be8 commit 9016fef

File tree

10 files changed

+18
-19
lines changed

10 files changed

+18
-19
lines changed

rust/kernel/amba.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<T: Driver> driver::DriverOps for Adapter<T> {
9595
}
9696
// SAFETY: By the safety requirements of this function, `reg` is valid and fully
9797
// initialised.
98-
to_result(|| unsafe { bindings::amba_driver_register(reg) })
98+
to_result(unsafe { bindings::amba_driver_register(reg) })
9999
}
100100

101101
unsafe fn unregister(reg: *mut bindings::amba_driver) {

rust/kernel/clk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Clk {
3535
/// This function should not be called in atomic context.
3636
pub fn prepare_enable(self) -> Result<EnabledClk> {
3737
// SAFETY: The pointer is valid by the type invariant.
38-
to_result(|| unsafe { bindings::clk_prepare_enable(self.0) })?;
38+
to_result(unsafe { bindings::clk_prepare_enable(self.0) })?;
3939
Ok(EnabledClk(self))
4040
}
4141
}

rust/kernel/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,9 @@ pub(crate) fn from_kernel_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
551551
Ok(ptr)
552552
}
553553

554-
/// Calls a kernel function that returns an integer error code on failure and converts the result
555-
/// to a [`Result`].
556-
pub fn to_result(func: impl FnOnce() -> core::ffi::c_int) -> Result {
557-
let err = func();
554+
/// Converts an integer as returned by a C kernel function to an error if it's negative, and
555+
/// `Ok(())` otherwise.
556+
pub fn to_result(err: core::ffi::c_int) -> Result {
558557
if err < 0 {
559558
Err(Error::from_kernel_errno(err))
560559
} else {

rust/kernel/hwrng.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<T: Operations> Registration<T> {
106106
);
107107

108108
// SAFETY: `bindings::hwrng` is initialized above which guarantees safety.
109-
to_result(|| unsafe { bindings::hwrng_register(this.hwrng.get()) })?;
109+
to_result(unsafe { bindings::hwrng_register(this.hwrng.get()) })?;
110110

111111
this.registered = true;
112112
this.name = Some(name);

rust/kernel/irq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl<T: PointerWrapper> InternalRegistration<T> {
366366
unsafe { T::from_pointer(ptr) };
367367
});
368368
// SAFETY: `name` and `ptr` remain valid as long as the registration is alive.
369-
to_result(|| unsafe {
369+
to_result(unsafe {
370370
bindings::request_threaded_irq(
371371
irq,
372372
handler,

rust/kernel/mm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub mod virt {
6666
// SAFETY: The page is guaranteed to be order 0 by the type system. The range of
6767
// `address` is already checked by `vm_insert_page`. `self.vma` and `page.pages` are
6868
// guaranteed by their repective type invariants to be valid.
69-
to_result(|| unsafe { bindings::vm_insert_page(self.vma, address as _, page.pages) })
69+
to_result(unsafe { bindings::vm_insert_page(self.vma, address as _, page.pages) })
7070
}
7171
}
7272

rust/kernel/net.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl TcpListener {
260260
};
261261

262262
// SAFETY: The namespace is valid and the output socket pointer is valid for write.
263-
to_result(|| unsafe {
263+
to_result(unsafe {
264264
bindings::sock_create_kern(
265265
ns.0.get(),
266266
pf as _,
@@ -275,10 +275,10 @@ impl TcpListener {
275275

276276
// SAFETY: The type invariant guarantees that the socket is valid, and `addr` and `addrlen`
277277
// were initialised based on valid values provided in the address enum.
278-
to_result(|| unsafe { bindings::kernel_bind(socket, addr, addrlen as _) })?;
278+
to_result(unsafe { bindings::kernel_bind(socket, addr, addrlen as _) })?;
279279

280280
// SAFETY: The socket is valid per the type invariant.
281-
to_result(|| unsafe { bindings::kernel_listen(socket, bindings::SOMAXCONN as _) })?;
281+
to_result(unsafe { bindings::kernel_listen(socket, bindings::SOMAXCONN as _) })?;
282282

283283
Ok(listener)
284284
}
@@ -295,7 +295,7 @@ impl TcpListener {
295295
let flags = if block { 0 } else { bindings::O_NONBLOCK };
296296
// SAFETY: The type invariant guarantees that the socket is valid, and the output argument
297297
// is also valid for write.
298-
to_result(|| unsafe { bindings::kernel_accept(self.sock, &mut new, flags as _) })?;
298+
to_result(unsafe { bindings::kernel_accept(self.sock, &mut new, flags as _) })?;
299299
Ok(TcpStream { sock: new })
300300
}
301301
}

rust/kernel/net/filter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<T: Filter> Registration<T> {
213213

214214
// SAFETY: `ns` has a valid reference to the namespace, and `this.hook` was just
215215
// initialised above, so they're both valid.
216-
to_result(|| unsafe { bindings::nf_register_net_hook(ns.0.get(), &this.hook) })?;
216+
to_result(unsafe { bindings::nf_register_net_hook(ns.0.get(), &this.hook) })?;
217217

218218
this.dev = dev;
219219
this.ns = Some(ns);

rust/kernel/platform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<T: Driver> driver::DriverOps for Adapter<T> {
4949
// - `probe()` and `remove()` are static functions.
5050
// - `of_match_table` is either a raw pointer with static lifetime,
5151
// as guaranteed by the [`driver::IdTable`] type, or null.
52-
to_result(|| unsafe { bindings::__platform_driver_register(reg, module.0) })
52+
to_result(unsafe { bindings::__platform_driver_register(reg, module.0) })
5353
}
5454

5555
unsafe fn unregister(reg: *mut bindings::platform_driver) {

rust/kernel/security.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ use crate::{bindings, cred::Credential, file::File, to_result, Result};
1010
/// context.
1111
pub fn binder_set_context_mgr(mgr: &Credential) -> Result {
1212
// SAFETY: `mrg.0` is valid because the shared reference guarantees a nonzero refcount.
13-
to_result(|| unsafe { bindings::security_binder_set_context_mgr(mgr.0.get()) })
13+
to_result(unsafe { bindings::security_binder_set_context_mgr(mgr.0.get()) })
1414
}
1515

1616
/// Calls the security modules to determine if binder transactions are allowed from task `from` to
1717
/// task `to`.
1818
pub fn binder_transaction(from: &Credential, to: &Credential) -> Result {
1919
// SAFETY: `from` and `to` are valid because the shared references guarantee nonzero refcounts.
20-
to_result(|| unsafe { bindings::security_binder_transaction(from.0.get(), to.0.get()) })
20+
to_result(unsafe { bindings::security_binder_transaction(from.0.get(), to.0.get()) })
2121
}
2222

2323
/// Calls the security modules to determine if task `from` is allowed to send binder objects
2424
/// (owned by itself or other processes) to task `to` through a binder transaction.
2525
pub fn binder_transfer_binder(from: &Credential, to: &Credential) -> Result {
2626
// SAFETY: `from` and `to` are valid because the shared references guarantee nonzero refcounts.
27-
to_result(|| unsafe { bindings::security_binder_transfer_binder(from.0.get(), to.0.get()) })
27+
to_result(unsafe { bindings::security_binder_transfer_binder(from.0.get(), to.0.get()) })
2828
}
2929

3030
/// Calls the security modules to determine if task `from` is allowed to send the given file to
3131
/// task `to` (which would get its own file descriptor) through a binder transaction.
3232
pub fn binder_transfer_file(from: &Credential, to: &Credential, file: &File) -> Result {
3333
// SAFETY: `from`, `to` and `file` are valid because the shared references guarantee nonzero
3434
// refcounts.
35-
to_result(|| unsafe {
35+
to_result(unsafe {
3636
bindings::security_binder_transfer_file(from.0.get(), to.0.get(), file.0.get())
3737
})
3838
}

0 commit comments

Comments
 (0)