Armando Montanez | 0bcae73 | 2020-05-27 13:28:11 -0700 | [diff] [blame] | 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 "pw_stream/memory_stream.h" |
| 16 | |
| 17 | #include <cstddef> |
| 18 | #include <cstring> |
| 19 | |
| 20 | #include "pw_status/status_with_size.h" |
| 21 | |
| 22 | namespace pw::stream { |
| 23 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 24 | Status MemoryWriter::DoWrite(std::span<const std::byte> data) { |
Armando Montanez | 0bcae73 | 2020-05-27 13:28:11 -0700 | [diff] [blame] | 25 | size_t bytes_to_write = |
| 26 | std::min(data.size_bytes(), dest_.size_bytes() - bytes_written_); |
| 27 | std::memcpy(dest_.data() + bytes_written_, data.data(), bytes_to_write); |
| 28 | bytes_written_ += bytes_to_write; |
| 29 | |
| 30 | if (bytes_to_write != data.size_bytes()) { |
| 31 | return Status::RESOURCE_EXHAUSTED; |
| 32 | } |
| 33 | return Status::OK; |
| 34 | } |
| 35 | |
| 36 | } // namespace pw::stream |