Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding str support back #369

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions core/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ pub enum TypeName {
pub enum StringEncoding {
UnvalidatedUtf8,
UnvalidatedUtf16,
/// The caller guarantees that they're passing valid UTF-8, under penalty of UB
Utf8,
}

impl TypeName {
Expand Down Expand Up @@ -514,6 +516,11 @@ impl TypeName {
))
.unwrap()
}
TypeName::StrReference(lifetime, StringEncoding::Utf8) => syn::parse_str(&format!(
"{}str",
ReferenceDisplay(lifetime, &Mutability::Immutable)
))
.unwrap(),
TypeName::PrimitiveSlice(lifetime, mutability, name) => {
let primitive_name = PRIMITIVE_TO_STRING.get(name).unwrap();
let formatted_str = format!(
Expand Down Expand Up @@ -549,14 +556,16 @@ impl TypeName {
let mutability = Mutability::from_syn(&r.mutability);

let name = r.elem.to_token_stream().to_string();
if name.starts_with("DiplomatStr") {
if name.starts_with("DiplomatStr") || name == "str" {
if mutability.is_mutable() {
panic!("mutable `DiplomatStr*` references are disallowed");
panic!("mutable string references are disallowed");
}
if name == "DiplomatStr" {
return TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf8);
} else if name == "DiplomatStr16" {
return TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf16);
} else if name == "str" {
return TypeName::StrReference(lifetime, StringEncoding::Utf8);
}
}
if let syn::Type::Slice(slice) = &*r.elem {
Expand Down Expand Up @@ -951,6 +960,13 @@ impl fmt::Display for TypeName {
ReferenceDisplay(lifetime, &Mutability::Immutable)
)
}
TypeName::StrReference(lifetime, StringEncoding::Utf8) => {
write!(
f,
"{}str",
ReferenceDisplay(lifetime, &Mutability::Immutable)
)
}
TypeName::PrimitiveSlice(lifetime, mutability, typ) => {
write!(f, "{}[{typ}]", ReferenceDisplay(lifetime, mutability))
}
Expand Down
1 change: 1 addition & 0 deletions feature_tests/c/include/BorrowedFields.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/c/include/MyString.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/c2/include/BorrowedFields.d.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/c2/include/MyString.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions feature_tests/cpp/docs/source/lifetimes_ffi.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions feature_tests/cpp/docs/source/slices_ffi.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/cpp/include/BorrowedFields.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/cpp/include/BorrowedFields.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion feature_tests/cpp/include/Foo.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/cpp/include/MyString.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions feature_tests/cpp/include/MyString.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/cpp2/include/BorrowedFields.d.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/cpp2/include/BorrowedFields.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions feature_tests/cpp2/include/BorrowedFields.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/cpp2/include/MyString.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/cpp2/include/MyString.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions feature_tests/cpp2/include/MyString.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion feature_tests/dart/lib/BorrowedFields.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions feature_tests/dart/lib/MyString.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions feature_tests/dotnet/Lib/Generated/MyString.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/dotnet/Lib/Generated/RawBorrowedFields.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions feature_tests/dotnet/Lib/Generated/RawMyString.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/js/api/BorrowedFields.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions feature_tests/js/api/BorrowedFields.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion feature_tests/js/api/Foo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions feature_tests/js/api/MyString.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions feature_tests/js/api/MyString.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/js/docs/source/lifetimes_ffi.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/js/docs/source/slices_ffi.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading