ptr-arrow 0.1.0

A macro to emulate C-style arrow syntax
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 4.18 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.04 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • meltah

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).