xref: /aosp_15_r20/external/skia/experimental/rust_png/ffi/UtilsForFFI.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 SkRustPngFfiUtils_DEFINED
9 #define SkRustPngFfiUtils_DEFINED
10 
11 #include "include/core/SkSpan.h"
12 
13 // TODO(https://crbug.com/356698922): Use a real `#include` if possible.
14 namespace rust {
15 inline namespace cxxbridge1 {
16 template <typename T> class Slice;
17 }  // namespace cxxbridge1
18 }  // namespace rust
19 
ToSkSpan(rust::Slice<T> slice)20 template <typename T> SkSpan<T> ToSkSpan(rust::Slice<T> slice) {
21     // Avoiding operating on `buffer.data()` if the slice is empty helps to avoid
22     // UB risk described at https://davidben.net/2024/01/15/empty-slices.html.
23     if (slice.empty()) {
24         return SkSpan<T>();
25     }
26 
27     return SkSpan<T>(slice.data(), slice.size());
28 }
29 
30 #endif  // SkRustPngFfiUtils_DEFINED
31