1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/// Gets the [`FieldOffset`] for the passed in type and (possibly nested) field.
///
/// This macro allows accessing private fields, following the normal Rust rules around privacy.
///
/// This macro doesn't allow accessing fields from type parameters in generic functions,
/// for that you can use `PUB_OFF`,
/// but it can only get the [`FieldOffset`] for public fields.
///
/// # Type Argument
///
/// The type parameter can be passed as either:
///
/// - The path to the type (including the type name)
///
/// - A type with generic arguments
///
/// ### Caveats with path argument
///
/// With the path way of passing the type,
/// if the type has all type parameters defaulted or is otherwise generic,
/// there can be type inference issues.
///
/// To fix type inference issues with defaulted types,
/// you can write `<>` (eg: `OFF!(for_examples::ReprC<>; a.b)`).
///
/// If a generic type is passed, and its arguments can be inferred from context,
/// it's only necessary to specify the type of the accessed field,
/// otherwise you need to write the full type.
///
/// # Example
///
/// ```rust
/// use repr_offset::{
/// for_examples::ReprC,
/// OFF,
/// FieldOffset, ROExtAcc,
/// };
///
/// let this = ReprC {a: 3u8, b: 5u8, c: 8u8, d: 13u8};
///
/// // Passing the type as a path
/// assert_eq!(OFF!(ReprC; a).get(&this), &this.a);
///
/// // Passing the type as a type
/// assert_eq!(OFF!(ReprC<_, _, _, _>; b).get(&this), &this.b);
///
/// // Passing the type as a path
/// assert_eq!(this.f_get(OFF!(ReprC; c)), &this.c);
///
/// // Passing the type as a type
/// assert_eq!(this.f_get(OFF!(ReprC<_, _, _, _>; d)), &this.d);
/// ```
///
/// [`FieldOffset`]: ./struct.FieldOffset.html
#[macro_export]
macro_rules! OFF{
(
$(:: $(@$leading:tt@)? )? $first:ident $(:: $trailing:ident)* ;
$($fields:tt).+
)=>{
$crate::__priv_OFF_path!(
[$(:: $($leading)?)? $first $(::$trailing)* ];
$($fields).+
)
};
($type:ty; $($fields:tt).+ )=>{unsafe{
let marker = $crate::utils::MakePhantomData::<$type>::FN_RET;
$crate::pmr::FOAssertStruct{
offset:{
use $crate::get_field_offset::r#unsafe::unsafe_get_private_field;
unsafe_get_private_field::<
_,
$crate::tstr::TS!($($fields),*)
>::__unsafe__GET_PRIVATE_FIELD_OFFSET
},
struct_: {
let _ = move || {
let variable = $crate::pmr::loop_create_mutref(marker);
#[allow(unused_unsafe)]
unsafe{ let _ = (*variable) $(.$fields)*; }
};
marker
},
}.offset
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __priv_OFF_path{
([$($path:tt)*]; $($fields:tt).+)=>{
$crate::pmr::FOAssertStruct{
offset:{
use $crate::get_field_offset::r#unsafe::unsafe_get_private_field;
unsafe_get_private_field::<
_,
$crate::tstr::TS!($($fields),*)
>::__unsafe__GET_PRIVATE_FIELD_OFFSET
},
struct_: {
use $crate::utils::AsPhantomData;
let marker = $($path)*::__REPR_OFFSET_PHANTOMDATA_FN;
let _ = move || {
let variable = $crate::pmr::loop_create_mutref(marker);
#[allow(unused_unsafe)]
unsafe{ let _ = (*variable) $(.$fields)*; }
};
marker
},
}.offset
}
}
/// Gets the [`FieldOffset`] for a (possibly nested) field, and an optionally passed in value.
///
/// The value argument is only necessary when the type that the fields are
/// from can't be inferred.
///
/// # Example
///
/// ```rust
/// use repr_offset::{
/// for_examples::ReprC,
/// off,
/// FieldOffset, ROExtAcc,
/// };
///
/// let this = ReprC {a: 3u8, b: 5u8, c: 8u8, d: 13u8};
///
/// // The value must be passed to the macro when you want to call
/// // any method on the returned `FieldOffset`.
/// assert_eq!(off!(this; a).get(&this), &this.a);
///
/// assert_eq!(this.f_get(off!(this; b)), &this.b);
///
/// assert_eq!(this.f_get(off!(c)), &this.c);
/// assert_eq!(this.f_get(off!(d)), &this.d);
/// ```
///
/// [`FieldOffset`]: ./struct.FieldOffset.html
#[macro_export]
macro_rules! off{
($value:expr; $($fields:tt).+ )=>{
$crate::pmr::FOAssertStruct{
offset:{
use $crate::get_field_offset::r#unsafe::unsafe_get_private_field;
unsafe_get_private_field::<
_,
$crate::tstr::TS!($($fields),*)
>::__unsafe__GET_PRIVATE_FIELD_OFFSET
},
struct_: {
use $crate::utils::AsPhantomData;
let mut marker = $crate::pmr::PhantomData;
if false {
let _ = $crate::utils::AsPhantomDataFn{
reference: &$value,
ty: marker,
};
let variable = $crate::pmr::loop_create_mutref(marker);
#[allow(unused_unsafe)]
unsafe{ let _ = (*variable) $(.$fields)*; }
}
marker
},
}.offset
};
( $($fields:tt).+ )=>{{
let marker = $crate::pmr::PhantomData;
if false {
$crate::pmr::loop_create_fo(marker)
} else {
let _ = || {
let value = $crate::pmr::loop_create_val(marker);
#[allow(unused_unsafe)]
unsafe{ let _ = value $(.$fields)*; }
};
type __Key = $crate::tstr::TS!($($fields),*);
use $crate::get_field_offset::r#unsafe::unsafe_get_private_field;
unsafe_get_private_field::<_,__Key>::__unsafe__GET_PRIVATE_FIELD_OFFSET
}
}};
}
/// Gets the [`FieldOffset`] for a (possibly nested) public field,
/// and an optionally passed in value.
///
/// This is the same as the [`off`] macro,
/// except that this can't access private fields,
/// and it allows accessing fields from type parameters in generic functions.
///
/// The value argument is only necessary when the type that the fields are
/// from can't be inferred.
///
/// # Examples
///
/// ### Named Type
///
/// ```rust
/// use repr_offset::{
/// for_examples::ReprC,
/// pub_off,
/// FieldOffset, ROExtAcc,
/// };
///
/// let this = ReprC {a: 3u8, b: 5u8, c: 8u8, d: 13u8};
///
/// // The value must be passed to the macro when you want to call
/// // any method on the returned `FieldOffset`.
/// assert_eq!(pub_off!(this; a).get(&this), &this.a);
///
/// assert_eq!(this.f_get(pub_off!(this; b)), &this.b);
///
/// assert_eq!(this.f_get(pub_off!(c)), &this.c);
/// assert_eq!(this.f_get(pub_off!(d)), &this.d);
/// ```
///
/// ### Accessing fields from type parameters
///
/// ```rust
/// use repr_offset::{
/// for_examples::ReprC,
/// tstr::TS,
/// pub_off,
/// FieldOffset, GetPubFieldOffset, ROExtOps,
/// };
///
/// let this = ReprC {a: 3u8, b: 5u8, c: 8u8, d: 13u8};
///
/// assertions(this);
///
/// fn assertions<T, A>(this: T)
/// where
/// T: GetPubFieldOffset<TS!(a), Type = u8, Alignment = A>,
/// T: GetPubFieldOffset<TS!(b), Type = u8, Alignment = A>,
/// T: GetPubFieldOffset<TS!(c), Type = u8, Alignment = A>,
/// T: GetPubFieldOffset<TS!(d), Type = u8, Alignment = A>,
/// T: ROExtOps<A>,
/// {
/// assert_eq!(this.f_get_copy(pub_off!(this; a)), 3);
/// assert_eq!(this.f_get_copy(pub_off!(this; b)), 5);
/// assert_eq!(this.f_get_copy(pub_off!(c)), 8);
/// assert_eq!(this.f_get_copy(pub_off!(d)), 13);
/// }
/// ```
///
/// [`off`]: ./macro.off.html
/// [`FieldOffset`]: ./struct.FieldOffset.html
///
#[macro_export]
macro_rules! pub_off{
($value:expr; $($fields:tt).+ )=>{
$crate::pmr::FOAssertStruct{
offset: $crate::pmr::GetPubFieldOffset::<$crate::tstr::TS!($($fields),*)>::OFFSET,
struct_: {
let mut marker = $crate::pmr::PhantomData;
if false {
let _ = $crate::utils::AsPhantomDataFn{
reference: &$value,
ty: marker,
};
}
marker
},
}.offset
};
( $($fields:tt).+ )=>{
$crate::pmr::GetPubFieldOffset::<$crate::tstr::TS!($($fields),*)>::OFFSET
};
}
/// Gets the [`FieldOffset`] for the passed in type and (possibly nested) public field.
///
/// This is the same as the [`OFF`] macro,
/// except that this can't access private fields,
/// and it allows accessing fields from type parameters in generic functions.
///
/// # Examples
///
/// ### Named Type
///
/// ```rust
/// use repr_offset::{
/// for_examples::ReprC,
/// PUB_OFF,
/// FieldOffset, ROExtAcc,
/// };
///
/// let this = ReprC {a: 3u8, b: 5u8, c: 8u8, d: 13u8};
///
/// // Passing the type as a path
/// assert_eq!(PUB_OFF!(ReprC; a).get(&this), &this.a);
///
/// // Passing the type as a type
/// assert_eq!(PUB_OFF!(ReprC<_, _, _, _>; b).get(&this), &this.b);
///
/// // Passing the type as a path
/// assert_eq!(this.f_get(PUB_OFF!(ReprC; c)), &this.c);
///
/// // Passing the type as a type
/// assert_eq!(this.f_get(PUB_OFF!(ReprC<_, _, _, _>; d)), &this.d);
/// ```
///
///
/// ### Accessing fields from type parameters
///
/// ```rust
/// use repr_offset::{
/// for_examples::ReprC,
/// tstr::TS,
/// PUB_OFF,
/// FieldOffset, GetPubFieldOffset, ROExtOps,
/// };
///
/// let this = ReprC {a: 3u8, b: 5u8, c: 8u8, d: 13u8};
///
/// assertions(this);
///
/// fn assertions<T, A>(this: T)
/// where
/// T: GetPubFieldOffset<TS!(a), Type = u8, Alignment = A>,
/// T: GetPubFieldOffset<TS!(b), Type = u8, Alignment = A>,
/// T: GetPubFieldOffset<TS!(c), Type = u8, Alignment = A>,
/// T: GetPubFieldOffset<TS!(d), Type = u8, Alignment = A>,
/// T: ROExtOps<A>,
/// {
/// assert_eq!(this.f_get_copy(PUB_OFF!(T; a)), 3);
/// assert_eq!(this.f_get_copy(PUB_OFF!(T; b)), 5);
/// assert_eq!(this.f_get_copy(PUB_OFF!(T; c)), 8);
/// assert_eq!(this.f_get_copy(PUB_OFF!(T; d)), 13);
/// }
/// ```
///
/// [`OFF`]: ./macro.OFF.html
/// [`FieldOffset`]: ./struct.FieldOffset.html
#[macro_export]
macro_rules! PUB_OFF{
(
$(:: $(@$leading:tt@)? )? $first:ident $(:: $trailing:ident)* ;
$($fields:tt).+
)=>{
$crate::__priv_ty_PUB_OFF_path!(
[$(:: $($leading)?)? $first $(::$trailing)* ];
$($fields).+
)
};
($type:ty; $($fields:tt).+ )=>{
<$type as $crate::pmr::GetPubFieldOffset::<$crate::tstr::TS!($($fields),*)>>::OFFSET
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __priv_ty_PUB_OFF_path{
([$($path:tt)*]; $($fields:tt).+)=>{
$crate::pmr::FOAssertStruct{
offset: $crate::pmr::GetPubFieldOffset::<$crate::tstr::TS!($($fields),*)>::OFFSET,
struct_: {
use $crate::utils::AsPhantomData;
$($path)*::__REPR_OFFSET_PHANTOMDATA_FN
}
}.offset
}
}