1*6777b538SAndroid Build Coastguard Worker // Copyright 2021 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_CONTAINERS_SPAN_RUST_H_ 6*6777b538SAndroid Build Coastguard Worker #define BASE_CONTAINERS_SPAN_RUST_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include <stdint.h> 9*6777b538SAndroid Build Coastguard Worker 10*6777b538SAndroid Build Coastguard Worker #include "base/containers/span.h" 11*6777b538SAndroid Build Coastguard Worker #include "base/rust_buildflags.h" 12*6777b538SAndroid Build Coastguard Worker #include "third_party/rust/cxx/v1/cxx.h" 13*6777b538SAndroid Build Coastguard Worker 14*6777b538SAndroid Build Coastguard Worker #if !BUILDFLAG(BUILD_RUST_BASE_CONVERSIONS) 15*6777b538SAndroid Build Coastguard Worker #error "span_rust.h included without BUILD_RUST_BASE_CONVERSIONS" 16*6777b538SAndroid Build Coastguard Worker #endif 17*6777b538SAndroid Build Coastguard Worker 18*6777b538SAndroid Build Coastguard Worker namespace base { 19*6777b538SAndroid Build Coastguard Worker 20*6777b538SAndroid Build Coastguard Worker // Create a Rust slice from a base::span. SpanToRustSlice(span<const uint8_t> span)21*6777b538SAndroid Build Coastguard Workerinline rust::Slice<const uint8_t> SpanToRustSlice(span<const uint8_t> span) { 22*6777b538SAndroid Build Coastguard Worker return rust::Slice<const uint8_t>(span.data(), span.size()); 23*6777b538SAndroid Build Coastguard Worker } 24*6777b538SAndroid Build Coastguard Worker 25*6777b538SAndroid Build Coastguard Worker // Note to future editors: if you add code to convert from a rust::Slice to 26*6777b538SAndroid Build Coastguard Worker // a base::span, you should be aware that Rust slices (and cxx) 27*6777b538SAndroid Build Coastguard Worker // return a fabricated non-null pointer for zero-length slices, and that 28*6777b538SAndroid Build Coastguard Worker // this will likely need converting to NULL before constructing a span. 29*6777b538SAndroid Build Coastguard Worker // cxx handles the conversion from NULL to an artificial pointer 30*6777b538SAndroid Build Coastguard Worker // (specifically it uses alignof<T>) in the C++ -> Rust direction, but 31*6777b538SAndroid Build Coastguard Worker // does nothing cunning in the other direction, presumably on the 32*6777b538SAndroid Build Coastguard Worker // assumption that C++ won't access the data pointer if the length is 0. 33*6777b538SAndroid Build Coastguard Worker // But we do not think (reinterpret_cast<T*>(alignof(T)), 0) can safely 34*6777b538SAndroid Build Coastguard Worker // be stored in base::span today. The pointer arithmetic 35*6777b538SAndroid Build Coastguard Worker // rules have special cases for NULL, but otherwise it is only defined if 36*6777b538SAndroid Build Coastguard Worker // there is actually an object at the address, and there is no object at 37*6777b538SAndroid Build Coastguard Worker // alignof(T). 38*6777b538SAndroid Build Coastguard Worker // https://eel.is/c++draft/expr.add#4 39*6777b538SAndroid Build Coastguard Worker // https://eel.is/c++draft/expr.add#5 40*6777b538SAndroid Build Coastguard Worker 41*6777b538SAndroid Build Coastguard Worker } // namespace base 42*6777b538SAndroid Build Coastguard Worker 43*6777b538SAndroid Build Coastguard Worker #endif // BASE_CONTAINERS_SPAN_RUST_H_ 44