coap_message/
helpers.rs

1//! Utility types needed for coap-message
2
3/// Reference that is annotated with a type ID corresponding to a 'static version of the type
4///
5/// How the ID corresponds to `T` is up to the type `T`. Any type may use it internally without
6/// documented stability guarantees (being itself responsible for upholding whatever it relies on).
7/// Code outside of the crate defining `T` should not construct it or rely on its properties; if
8/// really needed, that crates should provide own wrapper methods that define their precise
9/// unsafety criteria.
10pub struct RefWithStaticType<'a, T: ?Sized>(&'a T, core::any::TypeId);
11
12impl<'a, T: ?Sized> RefWithStaticType<'a, T> {
13    /// State that the given `id` is a type ID corresponding to T
14    ///
15    /// # Safety
16    ///
17    /// When it is safe to construct this depends on the semantics `T` ascribes to it.
18    pub unsafe fn new(reference: &'a T, id: core::any::TypeId) -> Self {
19        Self(reference, id)
20    }
21
22    /// Split into the contained data
23    pub fn into_inner(&self) -> (&'a T, core::any::TypeId) {
24        (self.0, self.1)
25    }
26}
27
28/// Exclusive reference that is annotated with a type ID corresponding to a 'static version of the
29/// type
30///
31/// How the ID corresponds to `T` is up to the type `T`. Any type may use it internally without
32/// documented stability guarantees (being itself responsible for upholding whatever it relies on).
33/// Code outside of the crate defining `T` should not construct it or rely on its properties; if
34/// really needed, that crates should provide own wrapper methods that define their precise
35/// unsafety criteria.
36pub struct RefMutWithStaticType<'a, T: ?Sized>(&'a mut T, core::any::TypeId);
37
38impl<'a, T: ?Sized> RefMutWithStaticType<'a, T> {
39    /// State that the given `id` is a type ID corresponding to T
40    ///
41    /// # Safety
42    ///
43    /// When it is safe to construct this depends on the semantics `T` ascribes to it.
44    pub unsafe fn new(reference: &'a mut T, id: core::any::TypeId) -> Self {
45        Self(reference, id)
46    }
47
48    /// Split into the contained data
49    pub fn into_inner(self) -> (&'a mut T, core::any::TypeId) {
50        (self.0, self.1)
51    }
52}