diff --git a/native/src/base/cxx_extern.rs b/native/src/base/cxx_extern.rs index 188026c375a3..be3d2b0b7fd0 100644 --- a/native/src/base/cxx_extern.rs +++ b/native/src/base/cxx_extern.rs @@ -6,8 +6,9 @@ use std::os::fd::{BorrowedFd, OwnedFd, RawFd}; use cxx::private::c_char; use libc::mode_t; +use crate::logging::CxxResultExt; pub(crate) use crate::xwrap::*; -use crate::{fd_path, map_fd, map_file, Directory, FsPath, ResultExt, Utf8CStr, Utf8CStrSlice}; +use crate::{fd_path, map_fd, map_file, Directory, FsPath, Utf8CStr, Utf8CStrSlice}; pub(crate) fn fd_path_for_cxx(fd: RawFd, buf: &mut [u8]) -> isize { let mut buf = Utf8CStrSlice::from(buf); diff --git a/native/src/base/logging.rs b/native/src/base/logging.rs index 28794cb6dd3c..f79d4c72eb29 100644 --- a/native/src/base/logging.rs +++ b/native/src/base/logging.rs @@ -10,12 +10,12 @@ use crate::{Utf8CStr, Utf8CStrArr}; // Error handling and logging throughout the Rust codebase in Magisk: // // All errors should be logged and consumed as soon as possible and converted into LoggedError. -// Implement `From for LoggedError` for non-standard error types so that we can -// directly use the `?` operator to propagate LoggedResult. +// For `Result` with errors that implement the `Display` trait, use the `?` operator to +// log and convert to LoggedResult. // // To log an error with more information, use `ResultExt::log_with_msg()`. // -// The "cxx" method variants in `ResultExt` are only used for C++ interop and +// The "cxx" method variants in `CxxResultExt` are only used for C++ interop and // should not be used directly in any Rust code. // // For general logging, use the !(...) macros. @@ -202,10 +202,38 @@ macro_rules! log_err { }}; } -pub trait ResultExt -where - Self: Sized, -{ +pub trait ResultExt { + fn log(self) -> LoggedResult; + fn log_with_msg fmt::Result>(self, f: F) -> LoggedResult; +} + +// Internal C++ bridging logging routines +pub(crate) trait CxxResultExt { + fn log_cxx(self) -> LoggedResult; + fn log_cxx_with_msg fmt::Result>(self, f: F) -> LoggedResult; +} + +trait LogImpl { + fn log_impl(self, level: LogLevel, caller: Option<&'static Location>) -> LoggedResult; + fn log_with_msg_impl fmt::Result>( + self, + level: LogLevel, + caller: Option<&'static Location>, + f: F, + ) -> LoggedResult; +} + +impl> CxxResultExt for R { + fn log_cxx(self) -> LoggedResult { + self.log_impl(LogLevel::ErrorCxx, None) + } + + fn log_cxx_with_msg fmt::Result>(self, f: F) -> LoggedResult { + self.log_with_msg_impl(LogLevel::ErrorCxx, None, f) + } +} + +impl> ResultExt for R { #[cfg(not(debug_assertions))] fn log(self) -> LoggedResult { self.log_impl(LogLevel::Error, None) @@ -227,25 +255,9 @@ where fn log_with_msg fmt::Result>(self, f: F) -> LoggedResult { self.log_with_msg_impl(LogLevel::Error, Some(Location::caller()), f) } - - fn log_cxx(self) -> LoggedResult { - self.log_impl(LogLevel::ErrorCxx, None) - } - - fn log_cxx_with_msg fmt::Result>(self, f: F) -> LoggedResult { - self.log_with_msg_impl(LogLevel::ErrorCxx, None, f) - } - - fn log_impl(self, level: LogLevel, caller: Option<&'static Location>) -> LoggedResult; - fn log_with_msg_impl fmt::Result>( - self, - level: LogLevel, - caller: Option<&'static Location>, - f: F, - ) -> LoggedResult; } -impl ResultExt for LoggedResult { +impl LogImpl for LoggedResult { fn log_impl(self, _: LogLevel, _: Option<&'static Location>) -> LoggedResult { self } @@ -272,7 +284,7 @@ impl ResultExt for LoggedResult { } } -impl ResultExt for Result { +impl LogImpl for Result { fn log_impl(self, level: LogLevel, caller: Option<&'static Location>) -> LoggedResult { match self { Ok(v) => Ok(v), diff --git a/native/src/base/xwrap.rs b/native/src/base/xwrap.rs index 3365f43eb4ff..e353d33fba4a 100644 --- a/native/src/base/xwrap.rs +++ b/native/src/base/xwrap.rs @@ -10,7 +10,7 @@ use libc::{ ssize_t, SYS_dup3, }; -use crate::{cstr, errno, raw_cstr, FsPath, ResultExt, Utf8CStr, Utf8CStrSlice}; +use crate::{cstr, errno, raw_cstr, CxxResultExt, FsPath, Utf8CStr, Utf8CStrSlice}; fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str { if ptr.is_null() {