From 42c960d582fb6d0bca3114009bb8a73dec453fdb Mon Sep 17 00:00:00 2001 From: chrysn Date: Sun, 19 Feb 2023 20:06:07 +0100 Subject: [PATCH] gcoap: Allow registration without scope for 'static listeners --- src/gcoap.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/gcoap.rs b/src/gcoap.rs index 1e08a817..4ebb98b0 100644 --- a/src/gcoap.rs +++ b/src/gcoap.rs @@ -33,7 +33,29 @@ where ret } -// Could we allow users the creation of 'static RegistrationScopes? Like thread::spawn. +/// Append a Gcoap listener in the global list of listeners, so that incoming requests are compared +/// to the listener's match functions and, if matching, are run through its handlers. +/// +/// Obtaining a static listener is relatively hard (in particular because storing it somewhere +/// static often requires naming its type, and that's both tedious until `type_alias_impl_trait` is +/// stabilized and hard with how handler generators like to return an impl trait). It is often +/// easier to construct them in a scoped fashion with [RegistrationScope::register]. +pub fn register

(listener: &'static mut P) +where + P: 'static + ListenerProvider, +{ + // Creating a scope out of thin air. This is OK because we're using it only on static data. + // + // RegistrationScope could conceivably have a public constructor for 'static 'static, but we're + // not exposing it that way because it'd be weird to create something as a scope that's not + // really scoping just to call that one function that we're providing here as a standalone + // function anyway. + let mut scope: RegistrationScope<'static, 'static> = RegistrationScope { + _phantom: PhantomData, + }; + scope.register(listener); +} + /// Lifetimed helper through which registrations can happen /// /// For explanations of the `'env`' and `'id` lifetimes, see