1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_span: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker======= 4*61c4878aSAndroid Build Coastguard Workerpw_span 5*61c4878aSAndroid Build Coastguard Worker======= 6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module:: 7*61c4878aSAndroid Build Coastguard Worker :name: pw_span 8*61c4878aSAndroid Build Coastguard Worker 9*61c4878aSAndroid Build Coastguard Worker* **Standardized**: :cpp:class:`pw::span` matches C++20's `std::span 10*61c4878aSAndroid Build Coastguard Worker <https://en.cppreference.com/w/cpp/container/span>`_ as closely as possible. 11*61c4878aSAndroid Build Coastguard Worker* **Zero-cost**: If ``std::span`` is available, ``pw::span`` is simply an alias 12*61c4878aSAndroid Build Coastguard Worker of it. 13*61c4878aSAndroid Build Coastguard Worker 14*61c4878aSAndroid Build Coastguard Worker.. pw_span-example-start 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 17*61c4878aSAndroid Build Coastguard Worker 18*61c4878aSAndroid Build Coastguard Worker #include <span> 19*61c4878aSAndroid Build Coastguard Worker 20*61c4878aSAndroid Build Coastguard Worker // With pw::span, a buffer is passed as a single argument. 21*61c4878aSAndroid Build Coastguard Worker // No need to handle pointer and size arguments. 22*61c4878aSAndroid Build Coastguard Worker bool ProcessBuffer(pw::span<uint8_t> buffer); 23*61c4878aSAndroid Build Coastguard Worker 24*61c4878aSAndroid Build Coastguard Worker bool DoStuff() { 25*61c4878aSAndroid Build Coastguard Worker // ... 26*61c4878aSAndroid Build Coastguard Worker ProcessBuffer(c_array); 27*61c4878aSAndroid Build Coastguard Worker ProcessBuffer(array_object); 28*61c4878aSAndroid Build Coastguard Worker ProcessBuffer(pw::span(data_pointer, data_size)); 29*61c4878aSAndroid Build Coastguard Worker } 30*61c4878aSAndroid Build Coastguard Worker 31*61c4878aSAndroid Build Coastguard Worker.. pw_span-example-end 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Worker:cpp:class:`pw::span` is a convenient abstraction that wraps a pointer and a 34*61c4878aSAndroid Build Coastguard Workersize. It's especially useful in APIs. Spans support implicit conversions from 35*61c4878aSAndroid Build Coastguard WorkerC arrays, ``std::array``, or any STL-style container, such as 36*61c4878aSAndroid Build Coastguard Worker``std::string_view``. 37*61c4878aSAndroid Build Coastguard Worker 38*61c4878aSAndroid Build Coastguard Worker.. _module-pw_span-start: 39*61c4878aSAndroid Build Coastguard Worker 40*61c4878aSAndroid Build Coastguard Worker----------- 41*61c4878aSAndroid Build Coastguard WorkerGet started 42*61c4878aSAndroid Build Coastguard Worker----------- 43*61c4878aSAndroid Build Coastguard Worker.. repository: https://bazel.build/concepts/build-ref#repositories 44*61c4878aSAndroid Build Coastguard Worker 45*61c4878aSAndroid Build Coastguard Worker.. tab-set:: 46*61c4878aSAndroid Build Coastguard Worker 47*61c4878aSAndroid Build Coastguard Worker .. tab-item:: Bazel 48*61c4878aSAndroid Build Coastguard Worker 49*61c4878aSAndroid Build Coastguard Worker Add ``@pigweed//pw_span`` to the ``deps`` list in your Bazel target: 50*61c4878aSAndroid Build Coastguard Worker 51*61c4878aSAndroid Build Coastguard Worker .. code-block:: 52*61c4878aSAndroid Build Coastguard Worker 53*61c4878aSAndroid Build Coastguard Worker cc_library("...") { 54*61c4878aSAndroid Build Coastguard Worker # ... 55*61c4878aSAndroid Build Coastguard Worker deps = [ 56*61c4878aSAndroid Build Coastguard Worker # ... 57*61c4878aSAndroid Build Coastguard Worker "@pigweed//pw_span", 58*61c4878aSAndroid Build Coastguard Worker # ... 59*61c4878aSAndroid Build Coastguard Worker ] 60*61c4878aSAndroid Build Coastguard Worker } 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard Worker This assumes that your Bazel ``WORKSPACE`` has a `repository 63*61c4878aSAndroid Build Coastguard Worker <https://bazel.build/concepts/build-ref#repositories>`_ named ``@pigweed`` 64*61c4878aSAndroid Build Coastguard Worker that points to the upstream Pigweed repository. 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Worker .. tab-item:: GN 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard Worker Add ``$dir_pw_span`` to the ``deps`` list in your ``pw_executable()`` 69*61c4878aSAndroid Build Coastguard Worker build target: 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard Worker .. code-block:: 72*61c4878aSAndroid Build Coastguard Worker 73*61c4878aSAndroid Build Coastguard Worker pw_executable("...") { 74*61c4878aSAndroid Build Coastguard Worker # ... 75*61c4878aSAndroid Build Coastguard Worker deps = [ 76*61c4878aSAndroid Build Coastguard Worker # ... 77*61c4878aSAndroid Build Coastguard Worker "$dir_pw_span", 78*61c4878aSAndroid Build Coastguard Worker # ... 79*61c4878aSAndroid Build Coastguard Worker ] 80*61c4878aSAndroid Build Coastguard Worker } 81*61c4878aSAndroid Build Coastguard Worker 82*61c4878aSAndroid Build Coastguard Worker .. tab-item:: CMake 83*61c4878aSAndroid Build Coastguard Worker 84*61c4878aSAndroid Build Coastguard Worker Add ``pw_span`` to your ``pw_add_library`` or similar CMake target: 85*61c4878aSAndroid Build Coastguard Worker 86*61c4878aSAndroid Build Coastguard Worker .. code-block:: 87*61c4878aSAndroid Build Coastguard Worker 88*61c4878aSAndroid Build Coastguard Worker pw_add_library(my_library STATIC 89*61c4878aSAndroid Build Coastguard Worker HEADERS 90*61c4878aSAndroid Build Coastguard Worker ... 91*61c4878aSAndroid Build Coastguard Worker PRIVATE_DEPS 92*61c4878aSAndroid Build Coastguard Worker # ... 93*61c4878aSAndroid Build Coastguard Worker pw_span 94*61c4878aSAndroid Build Coastguard Worker # ... 95*61c4878aSAndroid Build Coastguard Worker ) 96*61c4878aSAndroid Build Coastguard Worker 97*61c4878aSAndroid Build Coastguard Worker .. tab-item:: Zephyr 98*61c4878aSAndroid Build Coastguard Worker 99*61c4878aSAndroid Build Coastguard Worker There are two ways to use ``pw_span`` from a Zephyr project: 100*61c4878aSAndroid Build Coastguard Worker 101*61c4878aSAndroid Build Coastguard Worker #. Depend on ``pw_span`` in your CMake target (see CMake tab). This is 102*61c4878aSAndroid Build Coastguard Worker Pigweed Team's suggested approach since it enables precise CMake 103*61c4878aSAndroid Build Coastguard Worker dependency analysis. 104*61c4878aSAndroid Build Coastguard Worker 105*61c4878aSAndroid Build Coastguard Worker #. Add ``CONFIG_PIGWEED_SPAN=y`` to the Zephyr project's configuration, 106*61c4878aSAndroid Build Coastguard Worker which causes ``pw_span`` to become a global dependency and have the 107*61c4878aSAndroid Build Coastguard Worker includes exposed to all targets. Pigweed team does not recommend this 108*61c4878aSAndroid Build Coastguard Worker approach, though it is the typical Zephyr solution. 109*61c4878aSAndroid Build Coastguard Worker 110*61c4878aSAndroid Build Coastguard Worker------ 111*61c4878aSAndroid Build Coastguard WorkerGuides 112*61c4878aSAndroid Build Coastguard Worker------ 113*61c4878aSAndroid Build Coastguard Worker 114*61c4878aSAndroid Build Coastguard WorkerOperating on arrays of bytes 115*61c4878aSAndroid Build Coastguard Worker============================ 116*61c4878aSAndroid Build Coastguard WorkerWhen operating on an array of bytes you typically need pointer and size 117*61c4878aSAndroid Build Coastguard Workerarguments: 118*61c4878aSAndroid Build Coastguard Worker 119*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 120*61c4878aSAndroid Build Coastguard Worker 121*61c4878aSAndroid Build Coastguard Worker bool ProcessBuffer(char* buffer, size_t buffer_size); 122*61c4878aSAndroid Build Coastguard Worker 123*61c4878aSAndroid Build Coastguard Worker bool DoStuff() { 124*61c4878aSAndroid Build Coastguard Worker ProcessBuffer(c_array, sizeof(c_array)); 125*61c4878aSAndroid Build Coastguard Worker ProcessBuffer(array_object.data(), array_object.size()); 126*61c4878aSAndroid Build Coastguard Worker ProcessBuffer(data_pointer, data_size); 127*61c4878aSAndroid Build Coastguard Worker } 128*61c4878aSAndroid Build Coastguard Worker 129*61c4878aSAndroid Build Coastguard WorkerWith ``pw::span`` you can pass the buffer as a single argument instead: 130*61c4878aSAndroid Build Coastguard Worker 131*61c4878aSAndroid Build Coastguard Worker.. include:: docs.rst 132*61c4878aSAndroid Build Coastguard Worker :start-after: .. pw_span-example-start 133*61c4878aSAndroid Build Coastguard Worker :end-before: .. pw_span-example-end 134*61c4878aSAndroid Build Coastguard Worker 135*61c4878aSAndroid Build Coastguard WorkerWorking with spans of binary data 136*61c4878aSAndroid Build Coastguard Worker================================= 137*61c4878aSAndroid Build Coastguard WorkerUse ``pw::span<std::byte>`` or ``pw::span<const std::byte>`` to represent 138*61c4878aSAndroid Build Coastguard Workerspans of binary data. Convert spans to byte spans with ``pw::as_bytes`` or 139*61c4878aSAndroid Build Coastguard Worker``pw::as_writable_bytes``. 140*61c4878aSAndroid Build Coastguard Worker 141*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 142*61c4878aSAndroid Build Coastguard Worker 143*61c4878aSAndroid Build Coastguard Worker void ProcessData(pw::span<const std::byte> data); 144*61c4878aSAndroid Build Coastguard Worker 145*61c4878aSAndroid Build Coastguard Worker void DoStuff() { 146*61c4878aSAndroid Build Coastguard Worker std::array<AnyType, 7> data = { ... }; 147*61c4878aSAndroid Build Coastguard Worker ProcessData(pw::as_bytes(pw::span(data))); 148*61c4878aSAndroid Build Coastguard Worker } 149*61c4878aSAndroid Build Coastguard Worker 150*61c4878aSAndroid Build Coastguard Worker``pw_bytes/span.h`` provides ``ByteSpan`` and ``ConstByteSpan`` aliases for 151*61c4878aSAndroid Build Coastguard Workerthese types. 152*61c4878aSAndroid Build Coastguard Worker 153*61c4878aSAndroid Build Coastguard Worker---------- 154*61c4878aSAndroid Build Coastguard WorkerReferences 155*61c4878aSAndroid Build Coastguard Worker---------- 156*61c4878aSAndroid Build Coastguard Worker 157*61c4878aSAndroid Build Coastguard WorkerAPI reference 158*61c4878aSAndroid Build Coastguard Worker============= 159*61c4878aSAndroid Build Coastguard WorkerSee `std::span <https://en.cppreference.com/w/cpp/container/span>`_. 160*61c4878aSAndroid Build Coastguard WorkerThe ``pw::span`` API is designed to match the ``std::span`` API. 161*61c4878aSAndroid Build Coastguard Worker 162*61c4878aSAndroid Build Coastguard WorkerConfiguration options 163*61c4878aSAndroid Build Coastguard Worker===================== 164*61c4878aSAndroid Build Coastguard WorkerThe following configurations can be adjusted via compile-time configuration of 165*61c4878aSAndroid Build Coastguard Workerthis module, see the 166*61c4878aSAndroid Build Coastguard Worker:ref:`module documentation <module-structure-compile-time-configuration>` for 167*61c4878aSAndroid Build Coastguard Workermore details. 168*61c4878aSAndroid Build Coastguard Worker 169*61c4878aSAndroid Build Coastguard Worker.. doxygendefine:: PW_SPAN_ENABLE_ASSERTS 170