Skip to content

Commit

Permalink
gcoap: Allow registration without scope for 'static listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Feb 19, 2023
1 parent 9c29faf commit 42c960d
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/gcoap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P>(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
Expand Down

0 comments on commit 42c960d

Please sign in to comment.