temp-postgres 0.1.0

temporary postgres instance meant for unit tests
Documentation
use std::process::Child;

/// Simple wrapper around [`std::process::Child`] that kills the process when dropped.
pub struct KillOnDrop {
	/// The wrapped child process.
	child: Child,
}

impl KillOnDrop {
	/// Wrap an existing [`std::process:Child`] object.
	pub fn new(child: Child) -> Self {
		Self { child }
	}

	/// Get the PID of the child process.
	pub fn id(&self) -> u32 {
		self.child.id()
	}

	/// Kill the child process.
	pub fn kill(&mut self) -> std::io::Result<()> {
		self.child.kill()
	}

	// #[cfg(unix)]
	// /// Send a signal to the child process.
	// pub fn signal(&self, signal: i32) -> std::io::Result<()> {
	// 	// SAFETY: We're sending a signal to our own child process.
	// 	// Worst case, our child already exited,
	// 	// the PID has been reused and we send the signal to the wrong process.
	// 	unsafe {
	// 		if libc::kill(self.id() as _, signal) < 0 {
	// 			Err(std::io::Error::last_os_error())
	// 		} else {
	// 			Ok(())
	// 		}
	// 	}
	// }
}

impl Drop for KillOnDrop {
	fn drop(&mut self) {
		self.child.kill().ok();
		self.child.wait().ok();
	}
}