From 1125dafb43a95fb34be5e0b6e89678bf616fdb24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Hanuszczak?= Date: Wed, 30 Sep 2026 11:07:21 +0200 Subject: [PATCH 1/5] Enforce proper startup using the type system. --- crates/fleetspeak/examples/hello.rs | 4 +- crates/fleetspeak/src/lib.rs | 62 +++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/crates/fleetspeak/examples/hello.rs b/crates/fleetspeak/examples/hello.rs index 201ca0b..23d3e1a 100644 --- a/crates/fleetspeak/examples/hello.rs +++ b/crates/fleetspeak/examples/hello.rs @@ -11,10 +11,10 @@ fn main() { // SAFETY: We call `from_env` at the beginning of `main` so nothing could // have tampered with it. We also never invoke it again anywhere else. let fleetspeak = unsafe { - fleetspeak::Comms::from_env() + fleetspeak::UnstartedComms::from_env() }.expect("failed to handshake Fleetspeak connection"); - fleetspeak.startup("0.0.1") + let fleetspeak = fleetspeak.startup("0.0.1") .expect("failed to send Fleetspeak startup message"); for packet in fleetspeak.receiver() diff --git a/crates/fleetspeak/src/lib.rs b/crates/fleetspeak/src/lib.rs index fb618d2..0287a23 100644 --- a/crates/fleetspeak/src/lib.rs +++ b/crates/fleetspeak/src/lib.rs @@ -19,18 +19,24 @@ mod io; use std::sync::Mutex; use std::time::{Duration, Instant}; -/// Communication channel with the Fleetspeak process. -pub struct Comms { +/// Communication channel with the Fleetspeak process that did not send +/// startup information. +/// +/// The [`startup`] method should be used to send startup information and make +/// the communication channel ready for sending and receiving messages. +/// +/// [`startup`]: UnstartedComms::startup +pub struct UnstartedComms { // TODO(rust-lang/rust#134645): Migrate to `std::sync::nonpoison::Mutext` // (or `std::sync::ReentrantLock`) once stable. raw_out: Mutex, raw_in: Mutex, - last_heartbeat: Mutex>, } -impl Comms { +impl UnstartedComms { - // Returns a [`Comms`] instance given by the parent Fleetspeak process. + /// Returns a [`UnstartedComms`] instance given by the parent Fleetspeak + /// process. /// /// # Safety /// @@ -46,7 +52,7 @@ impl Comms { /// /// [1]: https://doc.rust-lang.org/std/os/fd/trait.AsRawFd.html#tymethod.as_raw_fd /// [2]: https://rust-lang.github.io/rfcs/3128-io-safety.html - pub unsafe fn from_env() -> std::io::Result { + pub unsafe fn from_env() -> std::io::Result { // SAFETY: Safety contract is the same as for the outer function. let mut raw_in = unsafe { crate::io::CommsInRaw::from_env() @@ -60,10 +66,9 @@ impl Comms { crate::io::handshake(&mut raw_in, &mut raw_out) .map_err(HandshakeError)?; - Ok(Comms { + Ok(UnstartedComms { raw_in: Mutex::new(raw_in), raw_out: Mutex::new(raw_out), - last_heartbeat: Mutex::new(None), }) } @@ -76,10 +81,30 @@ impl Comms { /// /// The `version` string should contain a self-reported version of the /// service. This data is used primarily for statistics. - pub fn startup(&self, version: &str) -> std::io::Result<()> { - let mut raw_out = self.raw_out.lock().unwrap(); - self::io::write_startup(&mut *raw_out, version) + pub fn startup(self, version: &str) -> std::io::Result { + { + let mut raw_out = self.raw_out.lock().unwrap(); + self::io::write_startup(&mut *raw_out, version)?; + } + + Ok(Comms { + raw_out: self.raw_out, + raw_in: self.raw_in, + last_heartbeat: Mutex::new(None), + }) } +} + +/// Communication channel with the Fleetspeak process. +pub struct Comms { + // TODO(rust-lang/rust#134645): Migrate to `std::sync::nonpoison::Mutext` + // (or `std::sync::ReentrantLock`) once stable. + raw_out: Mutex, + raw_in: Mutex, + last_heartbeat: Mutex>, +} + +impl Comms { /// Sends a heartbeat signal to the Fleetspeak client. /// @@ -135,7 +160,8 @@ impl Comms { /// ```no_run /// use fleetspeak::Message; /// - /// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = comms.startup("0.0.0").unwrap(); /// /// comms.send(Message { /// service: String::from("example"), @@ -168,7 +194,8 @@ impl Comms { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = comms.startup("0.0.0").unwrap(); /// /// for message in comms.receiver() /// .map(Result::unwrap) @@ -199,7 +226,8 @@ impl Comms { /// # Examples /// /// ```no_run - /// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = comms.startup("0.0.0").unwrap(); /// /// let message = comms.try_receive().unwrap() /// .expect("no message"); @@ -237,7 +265,8 @@ impl Comms { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = comms.startup("0.0.0").unwrap(); /// /// let message = comms.try_receive_with_heartbeat(Duration::from_secs(1)).unwrap() /// .expect("no message"); @@ -314,7 +343,8 @@ impl<'comms> Receiver<'comms> { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = comms.startup("0.0.0").unwrap(); /// /// for message in comms.receiver() /// .with_heartbeat(std::time::Duration::from_secs(1)) From f518d1de391e00d430df3ae36d40ad6526e98c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Hanuszczak?= Date: Wed, 30 Sep 2026 11:10:32 +0200 Subject: [PATCH 2/5] Use noun-adjective order for unstarted comms type. --- crates/fleetspeak/examples/hello.rs | 2 +- crates/fleetspeak/src/lib.rs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/fleetspeak/examples/hello.rs b/crates/fleetspeak/examples/hello.rs index 23d3e1a..b2529c7 100644 --- a/crates/fleetspeak/examples/hello.rs +++ b/crates/fleetspeak/examples/hello.rs @@ -11,7 +11,7 @@ fn main() { // SAFETY: We call `from_env` at the beginning of `main` so nothing could // have tampered with it. We also never invoke it again anywhere else. let fleetspeak = unsafe { - fleetspeak::UnstartedComms::from_env() + fleetspeak::CommsUnstarted::from_env() }.expect("failed to handshake Fleetspeak connection"); let fleetspeak = fleetspeak.startup("0.0.1") diff --git a/crates/fleetspeak/src/lib.rs b/crates/fleetspeak/src/lib.rs index 0287a23..c5f1bee 100644 --- a/crates/fleetspeak/src/lib.rs +++ b/crates/fleetspeak/src/lib.rs @@ -25,17 +25,17 @@ use std::time::{Duration, Instant}; /// The [`startup`] method should be used to send startup information and make /// the communication channel ready for sending and receiving messages. /// -/// [`startup`]: UnstartedComms::startup -pub struct UnstartedComms { +/// [`startup`]: CommsUnstarted::startup +pub struct CommsUnstarted { // TODO(rust-lang/rust#134645): Migrate to `std::sync::nonpoison::Mutext` // (or `std::sync::ReentrantLock`) once stable. raw_out: Mutex, raw_in: Mutex, } -impl UnstartedComms { +impl CommsUnstarted { - /// Returns a [`UnstartedComms`] instance given by the parent Fleetspeak + /// Returns a [`CommsUnstarted`] instance given by the parent Fleetspeak /// process. /// /// # Safety @@ -52,7 +52,7 @@ impl UnstartedComms { /// /// [1]: https://doc.rust-lang.org/std/os/fd/trait.AsRawFd.html#tymethod.as_raw_fd /// [2]: https://rust-lang.github.io/rfcs/3128-io-safety.html - pub unsafe fn from_env() -> std::io::Result { + pub unsafe fn from_env() -> std::io::Result { // SAFETY: Safety contract is the same as for the outer function. let mut raw_in = unsafe { crate::io::CommsInRaw::from_env() @@ -66,7 +66,7 @@ impl UnstartedComms { crate::io::handshake(&mut raw_in, &mut raw_out) .map_err(HandshakeError)?; - Ok(UnstartedComms { + Ok(CommsUnstarted { raw_in: Mutex::new(raw_in), raw_out: Mutex::new(raw_out), }) @@ -160,7 +160,7 @@ impl Comms { /// ```no_run /// use fleetspeak::Message; /// - /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// comms.send(Message { @@ -194,7 +194,7 @@ impl Comms { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// for message in comms.receiver() @@ -226,7 +226,7 @@ impl Comms { /// # Examples /// /// ```no_run - /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// let message = comms.try_receive().unwrap() @@ -265,7 +265,7 @@ impl Comms { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// let message = comms.try_receive_with_heartbeat(Duration::from_secs(1)).unwrap() @@ -343,7 +343,7 @@ impl<'comms> Receiver<'comms> { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::UnstartedComms::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// for message in comms.receiver() From 7d221d32b69085c4f7781587b82439a5422dd69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Hanuszczak?= Date: Wed, 30 Sep 2026 11:20:12 +0200 Subject: [PATCH 3/5] Make initialization a top-level function. --- crates/fleetspeak/examples/hello.rs | 2 +- crates/fleetspeak/src/lib.rs | 85 +++++++++++++++-------------- 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/crates/fleetspeak/examples/hello.rs b/crates/fleetspeak/examples/hello.rs index b2529c7..2aae8ad 100644 --- a/crates/fleetspeak/examples/hello.rs +++ b/crates/fleetspeak/examples/hello.rs @@ -11,7 +11,7 @@ fn main() { // SAFETY: We call `from_env` at the beginning of `main` so nothing could // have tampered with it. We also never invoke it again anywhere else. let fleetspeak = unsafe { - fleetspeak::CommsUnstarted::from_env() + fleetspeak::from_env() }.expect("failed to handshake Fleetspeak connection"); let fleetspeak = fleetspeak.startup("0.0.1") diff --git a/crates/fleetspeak/src/lib.rs b/crates/fleetspeak/src/lib.rs index c5f1bee..8151134 100644 --- a/crates/fleetspeak/src/lib.rs +++ b/crates/fleetspeak/src/lib.rs @@ -19,6 +19,44 @@ mod io; use std::sync::Mutex; use std::time::{Duration, Instant}; +/// Returns a [comms] instance given by the parent Fleetspeak process. +/// +/// [comms]: UnstartedComms +/// +/// # Safety +/// +/// This function must be invoked where the environment is guaranteed not to +/// have been tampered with (e.g. at the beginning of the `main` function), +/// so that it contains values really set by Fleetspeak (and not e.g. file +/// descriptors of Rust-allocated resources "leaked" by [`as_raw_fd`][1]). +/// +/// This is to adhere to [I/O safety][2] requirements. +/// +/// Synchronization is done on per-instance basis, so the users should +/// ensure that this is called only once in the program lifetime. +/// +/// [1]: https://doc.rust-lang.org/std/os/fd/trait.AsRawFd.html#tymethod.as_raw_fd +/// [2]: https://rust-lang.github.io/rfcs/3128-io-safety.html +pub unsafe fn from_env() -> std::io::Result { + // SAFETY: Safety contract is the same as for the outer function. + let mut raw_in = unsafe { + crate::io::CommsInRaw::from_env() + }.map_err(CommsInEnvError)?; + + // SAFETY: Safety contract is the same as for the outer function. + let mut raw_out = unsafe { + crate::io::CommsOutRaw::from_env() + }.map_err(CommsOutEnvError)?; + + crate::io::handshake(&mut raw_in, &mut raw_out) + .map_err(HandshakeError)?; + + Ok(CommsUnstarted { + raw_in: Mutex::new(raw_in), + raw_out: Mutex::new(raw_out), + }) +} + /// Communication channel with the Fleetspeak process that did not send /// startup information. /// @@ -35,43 +73,6 @@ pub struct CommsUnstarted { impl CommsUnstarted { - /// Returns a [`CommsUnstarted`] instance given by the parent Fleetspeak - /// process. - /// - /// # Safety - /// - /// This function must be invoked where the environment is guaranteed not to - /// have been tampered with (e.g. at the beginning of the `main` function), - /// so that it contains values really set by Fleetspeak (and not e.g. file - /// descriptors of Rust-allocated resources "leaked" by [`as_raw_fd`][1]). - /// - /// This is to adhere to [I/O safety][2] requirements. - /// - /// Synchronization is done on per-instance basis, so the users should - /// ensure that this is called only once in the program lifetime. - /// - /// [1]: https://doc.rust-lang.org/std/os/fd/trait.AsRawFd.html#tymethod.as_raw_fd - /// [2]: https://rust-lang.github.io/rfcs/3128-io-safety.html - pub unsafe fn from_env() -> std::io::Result { - // SAFETY: Safety contract is the same as for the outer function. - let mut raw_in = unsafe { - crate::io::CommsInRaw::from_env() - }.map_err(CommsInEnvError)?; - - // SAFETY: Safety contract is the same as for the outer function. - let mut raw_out = unsafe { - crate::io::CommsOutRaw::from_env() - }.map_err(CommsOutEnvError)?; - - crate::io::handshake(&mut raw_in, &mut raw_out) - .map_err(HandshakeError)?; - - Ok(CommsUnstarted { - raw_in: Mutex::new(raw_in), - raw_out: Mutex::new(raw_out), - }) - } - /// Sends a system message with startup information to the Fleetspeak /// client. /// @@ -160,7 +161,7 @@ impl Comms { /// ```no_run /// use fleetspeak::Message; /// - /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// comms.send(Message { @@ -194,7 +195,7 @@ impl Comms { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// for message in comms.receiver() @@ -226,7 +227,7 @@ impl Comms { /// # Examples /// /// ```no_run - /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// let message = comms.try_receive().unwrap() @@ -265,7 +266,7 @@ impl Comms { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// let message = comms.try_receive_with_heartbeat(Duration::from_secs(1)).unwrap() @@ -343,7 +344,7 @@ impl<'comms> Receiver<'comms> { /// ```no_run /// use std::time::Duration; /// - /// let comms = unsafe { fleetspeak::CommsUnstarted::from_env() }.unwrap(); + /// let comms = unsafe { fleetspeak::from_env() }.unwrap(); /// let comms = comms.startup("0.0.0").unwrap(); /// /// for message in comms.receiver() From 75beed8f02d4f063af1fde6f596fe6a2ec0fa313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Hanuszczak?= Date: Wed, 30 Sep 2026 11:22:37 +0200 Subject: [PATCH 4/5] Update the changelog file. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcc08a9..038e8b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Upcoming release ================ * Deleted deprecated API. + * Changed API to enforce sending startup information using the type system. 0.4.5 (2026-09-30) ================== From 68016b02f0d271ab8ef4b6addb8f80686fcfe984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Hanuszczak?= Date: Thu, 1 Oct 2026 20:29:22 +0200 Subject: [PATCH 5/5] Fix invalid docs reference. --- crates/fleetspeak/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fleetspeak/src/lib.rs b/crates/fleetspeak/src/lib.rs index 8151134..09490ba 100644 --- a/crates/fleetspeak/src/lib.rs +++ b/crates/fleetspeak/src/lib.rs @@ -21,7 +21,7 @@ use std::time::{Duration, Instant}; /// Returns a [comms] instance given by the parent Fleetspeak process. /// -/// [comms]: UnstartedComms +/// [comms]: CommsUnstarted /// /// # Safety ///