tokio/macros/
addr_of.rs

1//! This module defines a macro that lets you go from a raw pointer to a struct
2//! to a raw pointer to a field of the struct.
3
4macro_rules! generate_addr_of_methods {
5    (
6    impl<$($gen:ident)*> $struct_name:ty {$(
7        $(#[$attrs:meta])*
8        $vis:vis unsafe fn $fn_name:ident(self: NonNull<Self>) -> NonNull<$field_type:ty> {
9            &self$(.$field_name:tt)+
10        }
11    )*}
12    ) => {
13        impl<$($gen)*> $struct_name {$(
14            #[doc = "# Safety"]
15            #[doc = ""]
16            #[doc = "The `me` pointer must be valid."]
17            $(#[$attrs])*
18            $vis unsafe fn $fn_name(me: ::core::ptr::NonNull<Self>) -> ::core::ptr::NonNull<$field_type> {
19                let me = me.as_ptr();
20                // safety: the caller guarantees that `me` is valid
21                let field = unsafe { ::std::ptr::addr_of_mut!((*me) $(.$field_name)+ ) };
22                // safety: the field pointer is never null
23                unsafe { ::core::ptr::NonNull::new_unchecked(field) }
24            }
25        )*}
26    };
27}