| 1 |
/* |
| 2 |
* Copyright 2024 Google LLC. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkRustPngFFI_DEFINED |
| 9 |
#define SkRustPngFFI_DEFINED |
| 10 |
|
| 11 |
#include <stddef.h> |
| 12 |
#include <stdint.h> |
| 13 |
|
| 14 |
// TODO(https://crbug.com/356698922): Use a real `#include` if possible. |
| 15 |
namespace rust { |
| 16 |
inline namespace cxxbridge1 { |
| 17 |
template <typename T> class Slice; |
| 18 |
} // namespace cxxbridge1 |
| 19 |
} // namespace rust |
| 20 |
|
| 21 |
namespace rust_png { |
| 22 |
|
| 23 |
// Implementing the abstract C++ class below gives a rough equivalent of |
| 24 |
// `dyn std::io::Write` from Rust. |
| 25 |
class WriteTrait { |
| 26 |
public: |
| 27 |
// The `virtual` method below is a rough equivalent of the |
| 28 |
// `std::io::Write::write` method in Rust. See |
| 29 |
// https://doc.rust-lang.org/nightly/std/io/trait.Write.html#tymethod.write |
| 30 |
// for guidance on the desired implementation and behavior of this method. |
| 31 |
// |
| 32 |
// Implementation should return `true` if the whole buffer has been |
| 33 |
// successfully written (and return `false` to indicate an error). |
| 34 |
// This mimics how `SkStreamW::write` communicates errors. |
| 35 |
virtual bool write(rust::Slice<const uint8_t> buffer) = 0; |
| 36 |
|
| 37 |
// The `virtual` method below is a rough equivalent of the |
| 38 |
// `std::io::Write::flush` method in Rust. See |
| 39 |
// https://doc.rust-lang.org/nightly/std/io/trait.Write.html#tymethod.flush |
| 40 |
// for guidance on the desired implementation and behavior of this method. |
| 41 |
// |
| 42 |
// Note that (unlike in Rust) this method is infallible. This aspect of the |
| 43 |
// design mimics `SkStreamW::flush`. |
| 44 |
virtual void flush() = 0; |
| 45 |
|
| 46 |
// `drop` is not part of the `std::io::Write` trait, but we expose the |
| 47 |
// destructor through public API to make it possible to pass |
| 48 |
// `std::unique_ptr<WriteTrait>` over the FFI boundary. |
| 49 |
virtual ~WriteTrait() = default; |
| 50 |
|
| 51 |
// This type is non-copyable and non-movable. |
| 52 |
WriteTrait(const WriteTrait&) = delete; |
| 53 |
WriteTrait(WriteTrait&&) = delete; |
| 54 |
WriteTrait& operator=(const WriteTrait&) = delete; |
| 55 |
WriteTrait& operator=(WriteTrait&&) = delete; |
| 56 |
|
| 57 |
protected: |
| 58 |
WriteTrait() = default; |
| 59 |
}; |
| 60 |
|
| 61 |
} // namespace rust_png |
| 62 |
|
| 63 |
#endif // SkRustPngFFI_DEFINED |