Skip to content

Commit

Permalink
Adding str support back (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Nov 28, 2023
1 parent 5e392d5 commit a0586ac
Show file tree
Hide file tree
Showing 46 changed files with 268 additions and 63 deletions.
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

0 comments on commit a0586ac

Please sign in to comment.