1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include <cstring>
16 #include <span>
17
18 #include "pw_bytes/array.h"
19 #include "pw_log/log.h"
20 #include "pw_preprocessor/compiler.h"
21 #include "pw_result/result.h"
22
23 namespace {
24
25 // clang-format off
26 constexpr auto kArray = pw::bytes::Array<
27 0x0a, 0x14, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00,
28 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x96, 0x00,
29 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00, 0x12, 0x08,
30 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01>();
31 // clang-format on
32
Read(size_t offset,size_t size)33 PW_NO_INLINE pw::Result<std::span<const std::byte>> Read(size_t offset,
34 size_t size) {
35 if (offset + size >= std::size(kArray)) {
36 return pw::Status::OutOfRange();
37 }
38
39 return std::span<const std::byte>(std::data(kArray) + offset, size);
40 }
41
42 } // namespace
43
44 size_t volatile* unoptimizable;
45
main()46 int main() {
47 pw::Result result = Read(*unoptimizable, *unoptimizable);
48 if (!result.ok()) {
49 return 1;
50 }
51
52 PW_LOG_INFO("Read %u bytes", static_cast<unsigned>(result.value().size()));
53 return 0;
54 }
55