Debian Codegraph @

rust/common/SkStreamAdapter.h

raw
1 // Copyright 2024 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef SkStreamAdapter_DEFINED
6 #define SkStreamAdapter_DEFINED
7
8 #include "include/core/SkStream.h"
9 #include "include/private/base/SkAssert.h"
10 #include "rust/common/SpanUtils.h"
11
12 namespace rust {
13 namespace stream {
14
15 // Adapter class for reading and seeking Skia SkStreams from Rust.
16 // The caller needs to guarantee that `stream` will be alive for at least as
17 // long as the `SkStreamAdapter`.
18 class SkStreamAdapter {
19 public:
20 // The caller needs to guarantee that `stream` will be alive for at least as
21 // long as the `StreamAdapter`.
22 explicit SkStreamAdapter(SkStream* stream) : fStream(stream) {
23 SkASSERT_RELEASE(fStream);
24 }
25
26 ~SkStreamAdapter() = default;
27
28 // Non-copyable and non-movable (we want a stable `this` pointer)
29 SkStreamAdapter(const SkStreamAdapter&) = delete;
30 SkStreamAdapter& operator=(const SkStreamAdapter&) = delete;
31 SkStreamAdapter(SkStreamAdapter&&) = delete;
32 SkStreamAdapter& operator=(SkStreamAdapter&&) = delete;
33
34 // Read from stream into provided buffer. Returns number of bytes actually read.
35 size_t read(rust::Slice<uint8_t> buffer);
36
37 // Seek operations. Return true on success and set finalPos to actual position.
38 bool seek_from_start(uint64_t requestedPos, uint64_t& finalPos);
39 bool seek_from_end(int64_t requestedOffset, uint64_t& finalPos);
40 bool seek_relative(int64_t requestedOffset, uint64_t& finalPos);
41
42 private:
43 // TODO: Consider avoiding holding a raw / unowned pointer here:
44 // * Replace `fStream` field with an abstract method declaration:
45 // `virtual SkStream& stream() = 0`.
46 // * Remove the constructor.
47 // * Derive-from/implement this in a child class in `src/codec/SkPngRustCodec.cpp`
48 SkStream* fStream;
49 };
50
51 } // namespace stream
52 } // namespace rust
53
54 #endif // SkStreamAdapter_DEFINED