Change a few &Option<T> into Option<&T>

This commit is contained in:
Yuri Astrakhan 2024-09-28 01:46:03 -04:00
parent 18deb53874
commit f2d1edfea5
5 changed files with 20 additions and 19 deletions

View File

@ -78,9 +78,8 @@ fn io_err_to_addr(result: io::Result<&SocketAddr>) -> io::Result<String> {
} }
} }
fn addr_to_sockaddr(addr: &Option<String>) -> io::Result<SocketAddr> { fn addr_to_sockaddr(addr: Option<&str>) -> io::Result<SocketAddr> {
addr.as_ref() addr.ok_or(io::ErrorKind::AddrNotAvailable)?
.ok_or(io::ErrorKind::AddrNotAvailable)?
.to_socket_addrs() .to_socket_addrs()
// unwrap OK: if an iterator is returned, we're guaranteed to get exactly one entry // unwrap OK: if an iterator is returned, we're guaranteed to get exactly one entry
.map(|mut it| it.next().unwrap()) .map(|mut it| it.next().unwrap())
@ -161,11 +160,11 @@ pub fn is_write_vectored(&self) -> bool {
} }
pub fn peer_addr(&self) -> io::Result<SocketAddr> { pub fn peer_addr(&self) -> io::Result<SocketAddr> {
addr_to_sockaddr(&self.peer_addr) addr_to_sockaddr(self.peer_addr.as_deref())
} }
pub fn socket_addr(&self) -> io::Result<SocketAddr> { pub fn socket_addr(&self) -> io::Result<SocketAddr> {
addr_to_sockaddr(&self.inner.local_addr) addr_to_sockaddr(self.inner.local_addr.as_deref())
} }
pub fn shutdown(&self, _: Shutdown) -> io::Result<()> { pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
@ -255,13 +254,14 @@ pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
} }
pub fn socket_addr(&self) -> io::Result<SocketAddr> { pub fn socket_addr(&self) -> io::Result<SocketAddr> {
addr_to_sockaddr(&self.inner.local_addr) addr_to_sockaddr(self.inner.local_addr.as_deref())
} }
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?; let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?;
let peer_addr = Some(peer_addr); let peer_addr = Some(peer_addr);
let ret_peer = addr_to_sockaddr(&peer_addr).unwrap_or_else(|_| ([0; 4], 0).into()); let ret_peer =
addr_to_sockaddr(peer_addr.as_deref()).unwrap_or_else(|_| ([0; 4], 0).into());
Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer)) Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer))
} }

View File

@ -312,8 +312,8 @@ pub fn get_program_cstr(&self) -> &CStr {
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn get_cwd(&self) -> &Option<CString> { pub fn get_cwd(&self) -> Option<&CStr> {
&self.cwd self.cwd.as_deref()
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn get_uid(&self) -> Option<uid_t> { pub fn get_uid(&self) -> Option<uid_t> {

View File

@ -335,7 +335,7 @@ unsafe fn do_exec(
cvt(libc::setuid(u as uid_t))?; cvt(libc::setuid(u as uid_t))?;
} }
} }
if let Some(ref cwd) = *self.get_cwd() { if let Some(ref cwd) = self.get_cwd() {
cvt(libc::chdir(cwd.as_ptr()))?; cvt(libc::chdir(cwd.as_ptr()))?;
} }

View File

@ -650,8 +650,8 @@ fn run_test_in_process(
io::set_output_capture(None); io::set_output_capture(None);
let test_result = match result { let test_result = match result {
Ok(()) => calc_result(&desc, Ok(()), &time_opts, &exec_time), Ok(()) => calc_result(&desc, Ok(()), time_opts.as_ref(), exec_time.as_ref()),
Err(e) => calc_result(&desc, Err(e.as_ref()), &time_opts, &exec_time), Err(e) => calc_result(&desc, Err(e.as_ref()), time_opts.as_ref(), exec_time.as_ref()),
}; };
let stdout = data.lock().unwrap_or_else(|e| e.into_inner()).to_vec(); let stdout = data.lock().unwrap_or_else(|e| e.into_inner()).to_vec();
let message = CompletedTest::new(id, desc, test_result, exec_time, stdout); let message = CompletedTest::new(id, desc, test_result, exec_time, stdout);
@ -712,7 +712,8 @@ fn spawn_test_subprocess(
formatters::write_stderr_delimiter(&mut test_output, &desc.name); formatters::write_stderr_delimiter(&mut test_output, &desc.name);
test_output.extend_from_slice(&stderr); test_output.extend_from_slice(&stderr);
let result = get_result_from_exit_code(&desc, status, &time_opts, &exec_time); let result =
get_result_from_exit_code(&desc, status, time_opts.as_ref(), exec_time.as_ref());
(result, test_output, exec_time) (result, test_output, exec_time)
})(); })();
@ -724,8 +725,8 @@ fn run_test_in_spawned_subprocess(desc: TestDesc, runnable_test: RunnableTest) -
let builtin_panic_hook = panic::take_hook(); let builtin_panic_hook = panic::take_hook();
let record_result = Arc::new(move |panic_info: Option<&'_ PanicHookInfo<'_>>| { let record_result = Arc::new(move |panic_info: Option<&'_ PanicHookInfo<'_>>| {
let test_result = match panic_info { let test_result = match panic_info {
Some(info) => calc_result(&desc, Err(info.payload()), &None, &None), Some(info) => calc_result(&desc, Err(info.payload()), None, None),
None => calc_result(&desc, Ok(()), &None, &None), None => calc_result(&desc, Ok(()), None, None),
}; };
// We don't support serializing TrFailedMsg, so just // We don't support serializing TrFailedMsg, so just

View File

@ -42,8 +42,8 @@ pub enum TestResult {
pub fn calc_result<'a>( pub fn calc_result<'a>(
desc: &TestDesc, desc: &TestDesc,
task_result: Result<(), &'a (dyn Any + 'static + Send)>, task_result: Result<(), &'a (dyn Any + 'static + Send)>,
time_opts: &Option<time::TestTimeOptions>, time_opts: Option<&time::TestTimeOptions>,
exec_time: &Option<time::TestExecTime>, exec_time: Option<&time::TestExecTime>,
) -> TestResult { ) -> TestResult {
let result = match (&desc.should_panic, task_result) { let result = match (&desc.should_panic, task_result) {
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk, (&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
@ -96,8 +96,8 @@ pub fn calc_result<'a>(
pub fn get_result_from_exit_code( pub fn get_result_from_exit_code(
desc: &TestDesc, desc: &TestDesc,
status: ExitStatus, status: ExitStatus,
time_opts: &Option<time::TestTimeOptions>, time_opts: Option<&time::TestTimeOptions>,
exec_time: &Option<time::TestExecTime>, exec_time: Option<&time::TestExecTime>,
) -> TestResult { ) -> TestResult {
let result = match status.code() { let result = match status.code() {
Some(TR_OK) => TestResult::TrOk, Some(TR_OK) => TestResult::TrOk,