Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit de76914

Browse files
authoredOct 12, 2024
Rollup merge of rust-lang#130962 - nyurik:opts-libs, r=cuviper
Migrate lib's `&Option<T>` into `Option<&T>` Trying out my new lint rust-lang/rust-clippy#13336 - according to the [video](https://www.youtube.com/watch?v=6c7pZYP_iIE), this could lead to some performance and memory optimizations. Basic thoughts expressed in the video that seem to make sense: * `&Option<T>` in an API breaks encapsulation: * caller must own T and move it into an Option to call with it * if returned, the owner must store it as Option<T> internally in order to return it * Performance is subject to compiler optimization, but at the basics, `&Option<T>` points to memory that has `presence` flag + value, whereas `Option<&T>` by specification is always optimized to a single pointer.
2 parents 76ce3a9 + 7153288 commit de76914

File tree

6 files changed

+21
-20
lines changed

6 files changed

+21
-20
lines changed
 

‎std/src/sys/pal/sgx/net.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ fn io_err_to_addr(result: io::Result<&SocketAddr>) -> io::Result<String> {
7878
}
7979
}
8080

81-
fn addr_to_sockaddr(addr: &Option<String>) -> io::Result<SocketAddr> {
82-
addr.as_ref()
83-
.ok_or(io::ErrorKind::AddrNotAvailable)?
81+
fn addr_to_sockaddr(addr: Option<&str>) -> io::Result<SocketAddr> {
82+
addr.ok_or(io::ErrorKind::AddrNotAvailable)?
8483
.to_socket_addrs()
8584
// unwrap OK: if an iterator is returned, we're guaranteed to get exactly one entry
8685
.map(|mut it| it.next().unwrap())
@@ -161,11 +160,11 @@ impl TcpStream {
161160
}
162161

163162
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
164-
addr_to_sockaddr(&self.peer_addr)
163+
addr_to_sockaddr(self.peer_addr.as_deref())
165164
}
166165

167166
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
168-
addr_to_sockaddr(&self.inner.local_addr)
167+
addr_to_sockaddr(self.inner.local_addr.as_deref())
169168
}
170169

171170
pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
@@ -255,13 +254,14 @@ impl TcpListener {
255254
}
256255

257256
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
258-
addr_to_sockaddr(&self.inner.local_addr)
257+
addr_to_sockaddr(self.inner.local_addr.as_deref())
259258
}
260259

261260
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
262261
let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?;
263262
let peer_addr = Some(peer_addr);
264-
let ret_peer = addr_to_sockaddr(&peer_addr).unwrap_or_else(|_| ([0; 4], 0).into());
263+
let ret_peer =
264+
addr_to_sockaddr(peer_addr.as_deref()).unwrap_or_else(|_| ([0; 4], 0).into());
265265
Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer))
266266
}
267267

‎std/src/sys/pal/unix/process/process_common.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ impl Command {
312312
}
313313

314314
#[allow(dead_code)]
315-
pub fn get_cwd(&self) -> &Option<CString> {
316-
&self.cwd
315+
pub fn get_cwd(&self) -> Option<&CStr> {
316+
self.cwd.as_deref()
317317
}
318318
#[allow(dead_code)]
319319
pub fn get_uid(&self) -> Option<uid_t> {

‎std/src/sys/pal/unix/process/process_unix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ impl Command {
335335
cvt(libc::setuid(u as uid_t))?;
336336
}
337337
}
338-
if let Some(ref cwd) = *self.get_cwd() {
338+
if let Some(cwd) = self.get_cwd() {
339339
cvt(libc::chdir(cwd.as_ptr()))?;
340340
}
341341

‎std/src/sys/pal/unix/process/process_vxworks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Command {
5757
t!(cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO)));
5858
}
5959

60-
if let Some(ref cwd) = *self.get_cwd() {
60+
if let Some(cwd) = self.get_cwd() {
6161
t!(cvt(libc::chdir(cwd.as_ptr())));
6262
}
6363

‎test/src/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ fn run_test_in_process(
650650
io::set_output_capture(None);
651651

652652
let test_result = match result {
653-
Ok(()) => calc_result(&desc, Ok(()), &time_opts, &exec_time),
654-
Err(e) => calc_result(&desc, Err(e.as_ref()), &time_opts, &exec_time),
653+
Ok(()) => calc_result(&desc, Ok(()), time_opts.as_ref(), exec_time.as_ref()),
654+
Err(e) => calc_result(&desc, Err(e.as_ref()), time_opts.as_ref(), exec_time.as_ref()),
655655
};
656656
let stdout = data.lock().unwrap_or_else(|e| e.into_inner()).to_vec();
657657
let message = CompletedTest::new(id, desc, test_result, exec_time, stdout);
@@ -712,7 +712,8 @@ fn spawn_test_subprocess(
712712
formatters::write_stderr_delimiter(&mut test_output, &desc.name);
713713
test_output.extend_from_slice(&stderr);
714714

715-
let result = get_result_from_exit_code(&desc, status, &time_opts, &exec_time);
715+
let result =
716+
get_result_from_exit_code(&desc, status, time_opts.as_ref(), exec_time.as_ref());
716717
(result, test_output, exec_time)
717718
})();
718719

@@ -724,8 +725,8 @@ fn run_test_in_spawned_subprocess(desc: TestDesc, runnable_test: RunnableTest) -
724725
let builtin_panic_hook = panic::take_hook();
725726
let record_result = Arc::new(move |panic_info: Option<&'_ PanicHookInfo<'_>>| {
726727
let test_result = match panic_info {
727-
Some(info) => calc_result(&desc, Err(info.payload()), &None, &None),
728-
None => calc_result(&desc, Ok(()), &None, &None),
728+
Some(info) => calc_result(&desc, Err(info.payload()), None, None),
729+
None => calc_result(&desc, Ok(()), None, None),
729730
};
730731

731732
// We don't support serializing TrFailedMsg, so just

‎test/src/test_result.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ pub enum TestResult {
4242
pub fn calc_result<'a>(
4343
desc: &TestDesc,
4444
task_result: Result<(), &'a (dyn Any + 'static + Send)>,
45-
time_opts: &Option<time::TestTimeOptions>,
46-
exec_time: &Option<time::TestExecTime>,
45+
time_opts: Option<&time::TestTimeOptions>,
46+
exec_time: Option<&time::TestExecTime>,
4747
) -> TestResult {
4848
let result = match (&desc.should_panic, task_result) {
4949
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
@@ -96,8 +96,8 @@ pub fn calc_result<'a>(
9696
pub fn get_result_from_exit_code(
9797
desc: &TestDesc,
9898
status: ExitStatus,
99-
time_opts: &Option<time::TestTimeOptions>,
100-
exec_time: &Option<time::TestExecTime>,
99+
time_opts: Option<&time::TestTimeOptions>,
100+
exec_time: Option<&time::TestExecTime>,
101101
) -> TestResult {
102102
let result = match status.code() {
103103
Some(TR_OK) => TestResult::TrOk,

0 commit comments

Comments
 (0)
Please sign in to comment.