forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rust] Conversions from base <-> Rust types.
Most early Rust experiments will involve passing unstructured data into some Rust decoder or parser, then receiving structured data back. The unstructured data will typically be a base::span (containing binary data) or a base::StringPiece (containing textual data). Add conversions from these Chromium base types into the Rust equivalents, by means of the cxx types exposed that correspond to the Rust types. This CL does nothing in the case that Rust is disabled. If Rust is enabled, this will cause base_unittests to depend on the Rust standard library, because real Rust types are constructed by some of these C++ representations. Bug: 1069271 Change-Id: I1029bd435efd28a2c589b2560ea59357cb1ecd12 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2277259 Commit-Queue: Adrian Taylor <adetaylor@chromium.org> Reviewed-by: danakj <danakj@chromium.org> Reviewed-by: Robert Sesek <rsesek@chromium.org> Cr-Commit-Position: refs/heads/main@{#942443}
- Loading branch information
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// 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 "third_party/rust/cxx/v1/crate/include/cxx.h" | ||
|
||
namespace base { | ||
|
||
// Create a Rust slice from a base::span. | ||
inline rust::Slice<const uint8_t> SpanToRustSlice(span<const uint8_t> span) { | ||
return rust::Slice<const uint8_t>(span.data(), span.size()); | ||
} | ||
|
||
} // namespace base | ||
|
||
#endif // BASE_CONTAINERS_SPAN_RUST_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/containers/span_rust.h" | ||
|
||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace base { | ||
namespace { | ||
|
||
TEST(BaseSpanRustTest, SliceConstruct) { | ||
uint8_t data[] = {0, 1, 2, 3, 4}; | ||
span<const uint8_t> data_span(data, 2); | ||
rust::Slice<const uint8_t> rust_slice = SpanToRustSlice(data_span); | ||
EXPECT_EQ(2ul, rust_slice.length()); | ||
EXPECT_EQ(1, rust_slice[1]); | ||
} | ||
|
||
} // namespace | ||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_STRINGS_STRING_PIECE_RUST_H_ | ||
#define BASE_STRINGS_STRING_PIECE_RUST_H_ | ||
|
||
#include <stdint.h> | ||
|
||
#include "base/strings/string_piece.h" | ||
#include "third_party/rust/cxx/v1/crate/include/cxx.h" | ||
|
||
namespace base { | ||
|
||
// Create a Rust str from a base::BasigStringPiece. This will call std::abort | ||
// if there is any invalid UTF8. If you're concerned about this, then | ||
// instead use StringPieceToRustSlice and convert the data to a string on | ||
// the Rust side (or pass in a std::string). | ||
inline rust::Str StringPieceToRustStrUTF8(StringPiece string_piece) { | ||
return rust::Str(string_piece.data(), string_piece.size()); | ||
} | ||
|
||
// Create a Rust slice from a StringPiece. No UTF8 check is performed. | ||
inline rust::Slice<const uint8_t> StringPieceToRustSlice( | ||
StringPiece string_piece) { | ||
return rust::Slice<const uint8_t>( | ||
reinterpret_cast<const uint8_t*>(string_piece.data()), | ||
string_piece.length() * sizeof(StringPiece::value_type)); | ||
} | ||
|
||
// Create a StringPiece from a Rust str. | ||
inline StringPiece RustStrToStringPiece(rust::Str str) { | ||
return StringPiece(str.data(), str.size()); | ||
} | ||
|
||
} // namespace base | ||
|
||
#endif // BASE_STRINGS_STRING_PIECE_RUST_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/strings/string_piece_rust.h" | ||
|
||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace base { | ||
namespace { | ||
|
||
TEST(BaseStringPieceRustTest, StrRoundTrip) { | ||
std::string data = "hello"; | ||
StringPiece data_piece(data); | ||
rust::Str rust_str = StringPieceToRustStrUTF8(data_piece); | ||
EXPECT_EQ(5ul, rust_str.length()); | ||
StringPiece data_piece2 = RustStrToStringPiece(rust_str); | ||
EXPECT_EQ(data_piece, data_piece2); | ||
} | ||
|
||
TEST(BaseStringPieceRustTest, StrToSlice) { | ||
std::string data = "hello"; | ||
StringPiece data_piece(data); | ||
rust::Slice<const uint8_t> rust_slice = StringPieceToRustSlice(data_piece); | ||
EXPECT_EQ(5ul, rust_slice.length()); | ||
EXPECT_EQ('e', rust_slice[1]); | ||
} | ||
|
||
} // namespace | ||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters