File: span_rust.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (41 lines) | stat: -rw-r--r-- 1,594 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_CONTAINERS_SPAN_RUST_H_
#define BASE_CONTAINERS_SPAN_RUST_H_

#include <stdint.h>

#include "base/containers/span.h"
#include "build/build_config.h"
#include "third_party/rust/cxx/v1/cxx.h"

#if BUILDFLAG(IS_NACL)
#error "span_rust.h included under IS_NACL"
#endif

namespace base {

// Creates a Rust slice from a span.
inline rust::Slice<const uint8_t> SpanToRustSlice(span<const uint8_t> span) {
  return rust::Slice<const uint8_t>(span.data(), span.size());
}

// Note to future editors: if you add code to convert from a rust::Slice to a
// span, you should be aware that Rust slices (and cxx) return a fabricated
// non-null pointer for zero-length slices, and that this will likely need
// converting to NULL before constructing a span. cxx handles the conversion
// from NULL to an artificial pointer (specifically it uses alignof<T>) in the
// C++ -> Rust direction, but does nothing cunning in the other direction,
// presumably on the assumption that C++ won't access the data pointer if the
// length is 0. But we do not think (reinterpret_cast<T*>(alignof(T)), 0) can
// safely be stored in span today. The pointer arithmetic rules have special
// cases for NULL, but otherwise it is only defined if there is actually an
// object at the address, and there is no object at alignof(T).
// https://eel.is/c++draft/expr.add#4
// https://eel.is/c++draft/expr.add#5

}  // namespace base

#endif  // BASE_CONTAINERS_SPAN_RUST_H_