element_ptr!() { /* proc-macro */ }
Expand description

Returns the address of an inner element without created unneeded intermediate references.

The general syntax is

element_ptr!(base_ptr => /* element accesses */ )

The possible element accesses are:

  • . $field: Gets a pointer to the field specified by the field name of the struct behind the pointer.
  • . $index: Same as . $field but with a tuple index instead of a named struct field.
  • [ $index ]: Gets an element from a pointer to an array or slice at the specified index.
  • + $offset: Equivalent to pointer::add(). See its documentation for more info.
  • - $offset: Equivalent to pointer::sub(). See its documentation for more info.
  • u8+ $offset: Equivalent to pointer::byte_add(). See its documentation for more info.
  • u8- $offset: Equivalent to pointer::byte_sub(). See its documentation for more info.
  • as $type =>: Casts the pointer to a pointer with a pointee type of $type. If this is the last access within a group, the => may be omitted.
  • ( $accesses ): Groups accesses. Has no effect on the order in which accesses are applied, it just exists to allow for syntactic clarity.
  • .*: Reads the value behind the pointer. This should generally only be used for moving into a child pointer.

If some access returns a value that is not a pointer (meaning .* or a group containing it as the last access), it will be a compiler error to have any accesses afterwards.

Safety

  • Every intermediate pointer and the final pointer must remain within the bounds of the same allocated object. See pointer::offset() for more information.
  • The .* element access unconditionally reads the value from memory. See read() for more information.
  • Aside from .*, all other element accesses do not read from the memory they are pointing to. They also do not create intermediate references.