From 4b9767ce837c520fc524ffb8621d90dfb2ae6e4c Mon Sep 17 00:00:00 2001 From: Peter Ammon Date: Fri, 27 Dec 2024 14:22:31 -0800 Subject: [PATCH] Remove as_ptr from IoData We don't need this. Also improve IoChain::remove(). --- src/io.rs | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/io.rs b/src/io.rs index 73e29d0cb..d68da89dc 100644 --- a/src/io.rs +++ b/src/io.rs @@ -178,8 +178,8 @@ pub trait IoData: Send + Sync { /// That is, we call dup2(source_fd, fd). fn source_fd(&self) -> RawFd; fn print(&self); - // The address of the object, for comparison. - fn as_ptr(&self) -> *const (); + + // If this is an IoBufferfill, return a reference to it. fn as_bufferfill(&self) -> Option<&IoBufferfill> { None } @@ -206,9 +206,6 @@ fn source_fd(&self) -> RawFd { fn print(&self) { eprintf!("close %d\n", self.fd) } - fn as_ptr(&self) -> *const () { - (self as *const Self).cast() - } } pub struct IoFd { @@ -235,9 +232,6 @@ fn source_fd(&self) -> RawFd { fn print(&self) { eprintf!("FD map %d -> %d\n", self.source_fd, self.fd) } - fn as_ptr(&self) -> *const () { - (self as *const Self).cast() - } } /// Represents a redirection to or from an opened file. @@ -267,9 +261,6 @@ fn source_fd(&self) -> RawFd { fn print(&self) { eprintf!("file %d -> %d\n", self.file.as_raw_fd(), self.fd) } - fn as_ptr(&self) -> *const () { - (self as *const Self).cast() - } } /// Represents (one end) of a pipe. @@ -307,9 +298,6 @@ fn print(&self) { self.fd ) } - fn as_ptr(&self) -> *const () { - (self as *const Self).cast() - } } /// Represents filling an IoBuffer. Very similar to IoPipe. @@ -393,9 +381,6 @@ fn print(&self) { self.fd() ) } - fn as_ptr(&self) -> *const () { - (self as *const Self).cast() - } fn as_bufferfill(&self) -> Option<&IoBufferfill> { Some(self) } @@ -512,12 +497,14 @@ pub fn new() -> Self { Default::default() } pub fn remove(&mut self, element: &dyn IoData) { - let element = element as *const _; - let element = element as *const (); - self.0.retain(|e| { - let e = Arc::as_ptr(e) as *const (); - !std::ptr::eq(e, element) - }); + // Discard vtable pointers when comparing. + let e1 = element as *const dyn IoData as *const (); + let idx = self + .0 + .iter() + .position(|e2| Arc::as_ref(e2) as *const dyn IoData as *const () == e1) + .expect("Element not found"); + self.0.remove(idx); } pub fn clear(&mut self) { self.0.clear()