Skip to main content

Crate ptr_arrow

Crate ptr_arrow 

Source
Expand description

A macro to emulate C-style arrow syntax.

§Usage

arrow!(my_pointer->field)

…which expands to:

(*my_pointer).field

Arrows can be chained, and arrows and regular field accesses can be intermixed:

arrow!(my_pointer->my_pair.0->foo)

Arbitrary expressions are supported:

arrow!((if condition { some_func() } else { some_other_func() })->foo.bar()->baz)

§Indexing

Pointers can be indexed using the syntax my_pointer[->index], where my_pointer is a *const T or *mut T. Since no bounds checks can be performed, you must ensure that the index is in bounds for the pointer.

§Method calls

If your struct has a method taking in a raw pointer as the first argument, you can call it with a special construct:

struct Thing { ... }
impl Thing {
	fn do_stuff(this: *mut Self, n: i32) { ... }
}

arrow!(root->my_thing->Thing::do_stuff(5));

…which becomes:

Thing::do_stuff((*root).my_thing, 5)

This only works if a full path is specified; arrow!(root->my_thing->do_stuff()) will simply desugar to (*(*root).my_thing).do_stuff().

§Note on precedence

The -> operator in this macro is extremely loosely binding; all other operators take precedence over it. arrow!(a + b->foo) is interpreted as arrow!((a + b)->foo). If you want to express adding a and b->foo, you should use a + arrow!(b->foo).

Macros§

arrow
Wraps an expression with C-style arrow syntax.