Skip to content

Commit

Permalink
Merge pull request #1185 from bendk/askama-0.11
Browse files Browse the repository at this point in the history
Update to Askama 0.11
  • Loading branch information
bendk authored Mar 11, 2022
2 parents 419a1cd + 4298d5d commit 12fb0cd
Show file tree
Hide file tree
Showing 55 changed files with 199 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ commands:
# Our minimum supported rust version is specified here.
prepare-rust-min-version:
steps:
- run: rustup override set 1.47.0
- run: rustup override set 1.57.0
- run: rustup update
build-api-docs:
steps:
Expand Down
4 changes: 1 addition & 3 deletions fixtures/coverall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ impl Coveralls {
}

fn get_other(&self) -> Option<Arc<Self>> {
(*self.other.lock().unwrap())
.as_ref()
.map(|other| Arc::clone(other))
(*self.other.lock().unwrap()).as_ref().map(Arc::clone)
}

fn take_other_fallible(self: Arc<Self>) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ pub struct RecursivelyUsesU8 {

#[derive(Debug)]
pub struct InterfaceUsingUnsigned {
member: u64,
_member: u64,
}

impl InterfaceUsingUnsigned {
pub fn new(new: u64) -> Self {
Self { member: new }
Self { _member: new }
}

fn uses_unsigned_struct(&self, mut p1: RecursivelyUsesU8) {
Expand Down
42 changes: 35 additions & 7 deletions fixtures/uitests/tests/ui/interface_not_sync_and_send.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@ error[E0277]: `Cell<u32>` cannot be shared between threads safely
--> $OUT_DIR[uniffi_uitests]/counter.uniffi.rs
|
| uniffi::deps::static_assertions::assert_impl_all!(Counter: Sync, Send);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `Cell<u32>` cannot be shared between threads safely
| required by this bound in `assert_impl_all`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Cell<u32>` cannot be shared between threads safely
|
= help: within `Counter`, the trait `Sync` is not implemented for `Cell<u32>`
note: required because it appears within the type `Counter`
--> tests/ui/interface_not_sync_and_send.rs:9:12
|
9 | pub struct Counter {
| ^^^^^^^
note: required by a bound in `assert_impl_all`
--> $OUT_DIR[uniffi_uitests]/counter.uniffi.rs
|
| uniffi::deps::static_assertions::assert_impl_all!(Counter: Sync, Send);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `assert_impl_all`
= note: this error originates in the macro `uniffi::deps::static_assertions::assert_impl_all` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `Arc<Counter>: FfiConverter` is not satisfied
--> $OUT_DIR[uniffi_uitests]/counter.uniffi.rs
|
| <std::sync::Arc<Counter> as uniffi::FfiConverter>::lower(_arc)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FfiConverter` is not implemented for `Arc<Counter>`
|
= help: the following implementations were found:
<Arc<T> as FfiConverter>
note: required by `lower`
--> $WORKSPACE/uniffi/src/lib.rs
|
| fn lower(obj: Self::RustType) -> Self::FfiType;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `Arc<Counter>: FfiConverter` is not satisfied
--> $OUT_DIR[uniffi_uitests]/counter.uniffi.rs
|
| <std::sync::Arc<Counter> as uniffi::FfiConverter>::lower(_arc)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FfiConverter` is not implemented for `Arc<Counter>`
| ^^^^ the trait `FfiConverter` is not implemented for `Arc<Counter>`
|
= help: the following implementations were found:
<Arc<T> as FfiConverter>
= note: required by `lower`

error[E0277]: the trait bound `Arc<Counter>: FfiConverter` is not satisfied
--> $OUT_DIR[uniffi_uitests]/counter.uniffi.rs
Expand All @@ -33,4 +48,17 @@ error[E0277]: the trait bound `Arc<Counter>: FfiConverter` is not satisfied
|
= help: the following implementations were found:
<Arc<T> as FfiConverter>
= note: required by `try_lift`
note: required by `try_lift`
--> $WORKSPACE/uniffi/src/lib.rs
|
| fn try_lift(v: Self::FfiType) -> Result<Self::RustType>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `Arc<Counter>: FfiConverter` is not satisfied
--> $OUT_DIR[uniffi_uitests]/counter.uniffi.rs
|
| match<std::sync::Arc<Counter> as uniffi::FfiConverter>::try_lift(ptr) {
| ^^^ the trait `FfiConverter` is not implemented for `Arc<Counter>`
|
= help: the following implementations were found:
<Arc<T> as FfiConverter>
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# * ./.circleci/config.yml which also specifies the rust versions used in CI.

[toolchain]
channel = "1.54.0"
channel = "1.57.0"
targets = [
"aarch64-linux-android",
"armv7-linux-androideabi",
Expand Down
4 changes: 2 additions & 2 deletions uniffi/tests/ui/version_mismatch.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error[E0080]: evaluation of constant value failed
--> $DIR/version_mismatch.rs:2:1
--> tests/ui/version_mismatch.rs:2:1
|
2 | uniffi::assert_compatible_version!("0.0.1"); // An error message would go here.
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
|
= note: this error originates in the macro `uniffi::deps::static_assertions::const_assert` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion uniffi_bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "src/main.rs"
cargo_metadata = "0.13"
weedle = "0.12"
anyhow = "1"
askama = { version = "0.10", default-features = false, features = ["config"] }
askama = { version = "0.11", default-features = false, features = ["config"] }
heck = "0.3"
clap = { version = "2", default-features = false }
paste = "1.0"
Expand Down
11 changes: 5 additions & 6 deletions uniffi_bindgen/src/backend/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use super::{CodeType, TypeIdentifier};
use crate::interface::FFIType;
use std::fmt;

/// An object to look up a foreign language code specific renderer for a given type used.
/// Every `Type` referred to in the `ComponentInterface` should map to a corresponding
Expand All @@ -18,19 +17,19 @@ pub trait CodeOracle {
fn find(&self, type_: &TypeIdentifier) -> Box<dyn CodeType>;

/// Get the idiomatic rendering of a class name (for enums, records, errors, etc).
fn class_name(&self, nm: &dyn fmt::Display) -> String;
fn class_name(&self, nm: &str) -> String;

/// Get the idiomatic rendering of a function name.
fn fn_name(&self, nm: &dyn fmt::Display) -> String;
fn fn_name(&self, nm: &str) -> String;

/// Get the idiomatic rendering of a variable name.
fn var_name(&self, nm: &dyn fmt::Display) -> String;
fn var_name(&self, nm: &str) -> String;

/// Get the idiomatic rendering of an individual enum variant.
fn enum_variant_name(&self, nm: &dyn fmt::Display) -> String;
fn enum_variant_name(&self, nm: &str) -> String;

/// Get the idiomatic rendering of an error name.
fn error_name(&self, nm: &dyn fmt::Display) -> String;
fn error_name(&self, nm: &str) -> String;

fn ffi_type_label(&self, ffi_type: &FFIType) -> String;
}
29 changes: 9 additions & 20 deletions uniffi_bindgen/src/backend/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

use super::{CodeOracle, Literal};
use crate::interface::*;
use std::fmt;

/// A Trait to emit foreign language code to handle referenced types.
/// A type which is specified in the UDL (i.e. a member of the component interface)
Expand Down Expand Up @@ -73,33 +72,28 @@ pub trait CodeType {
/// An expression for lowering a value into something we can pass over the FFI.
///
/// N.B. This should align with the `helper_code` generated by this `CodeType`.
fn lower(&self, oracle: &dyn CodeOracle, _nm: &dyn fmt::Display) -> String {
fn lower(&self, oracle: &dyn CodeOracle, _nm: &str) -> String {
unimplemented!("Unimplemented for {}", self.type_label(oracle))
}

/// An expression for writing a value into a byte buffer.
///
/// N.B. This should align with the `helper_code` generated by this `CodeType`.
fn write(
&self,
oracle: &dyn CodeOracle,
_nm: &dyn fmt::Display,
_target: &dyn fmt::Display,
) -> String {
fn write(&self, oracle: &dyn CodeOracle, _nm: &str, _target: &str) -> String {
unimplemented!("Unimplemented for {}", self.type_label(oracle))
}

/// An expression for lifting a value from something we received over the FFI.
///
/// N.B. This should align with the `helper_code` generated by this `CodeType`.
fn lift(&self, oracle: &dyn CodeOracle, _nm: &dyn fmt::Display) -> String {
fn lift(&self, oracle: &dyn CodeOracle, _nm: &str) -> String {
unimplemented!("Unimplemented for {}", self.type_label(oracle))
}

/// An expression for reading a value from a byte buffer.
///
/// N.B. This should align with the `helper_code` generated by this `CodeType`.
fn read(&self, oracle: &dyn CodeOracle, _nm: &dyn fmt::Display) -> String {
fn read(&self, oracle: &dyn CodeOracle, _nm: &str) -> String {
unimplemented!("Unimplemented for {}", self.type_label(oracle))
}

Expand All @@ -119,7 +113,7 @@ pub trait CodeType {
}

/// An expression to coerce the given variable to the expected type.
fn coerce(&self, oracle: &dyn CodeOracle, _nm: &dyn fmt::Display) -> String {
fn coerce(&self, oracle: &dyn CodeOracle, _nm: &str) -> String {
panic!("Unimplemented for {}", self.type_label(oracle));
}
}
Expand Down Expand Up @@ -206,24 +200,19 @@ impl<T: CodeTypeDispatch> CodeType for T {
self.code_type_impl(oracle).literal(oracle, literal)
}

fn lower(&self, oracle: &dyn CodeOracle, nm: &dyn fmt::Display) -> String {
fn lower(&self, oracle: &dyn CodeOracle, nm: &str) -> String {
self.code_type_impl(oracle).lower(oracle, nm)
}

fn write(
&self,
oracle: &dyn CodeOracle,
nm: &dyn fmt::Display,
target: &dyn fmt::Display,
) -> String {
fn write(&self, oracle: &dyn CodeOracle, nm: &str, target: &str) -> String {
self.code_type_impl(oracle).write(oracle, nm, target)
}

fn lift(&self, oracle: &dyn CodeOracle, nm: &dyn fmt::Display) -> String {
fn lift(&self, oracle: &dyn CodeOracle, nm: &str) -> String {
self.code_type_impl(oracle).lift(oracle, nm)
}

fn read(&self, oracle: &dyn CodeOracle, nm: &dyn fmt::Display) -> String {
fn read(&self, oracle: &dyn CodeOracle, nm: &str) -> String {
self.code_type_impl(oracle).read(oracle, nm)
}

Expand Down
1 change: 1 addition & 0 deletions uniffi_bindgen/src/bindings/kotlin/gen_kotlin/compounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::filters;
use crate::backend::{CodeOracle, CodeType, Literal, TypeIdentifier};
use askama::Template;
use paste::paste;
use std::borrow::Borrow;

fn render_literal(oracle: &dyn CodeOracle, literal: &Literal, inner: &TypeIdentifier) -> String {
match literal {
Expand Down
1 change: 1 addition & 0 deletions uniffi_bindgen/src/bindings/kotlin/gen_kotlin/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{filters, CustomTypeConfig};
use crate::backend::{CodeDeclaration, CodeOracle, CodeType, Literal};
use crate::interface::{FFIType, Type};
use askama::Template;
use std::borrow::Borrow;

pub struct CustomCodeType {
name: String,
Expand Down
1 change: 1 addition & 0 deletions uniffi_bindgen/src/bindings/kotlin/gen_kotlin/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::backend::{CodeDeclaration, CodeOracle, CodeType, Literal, TypeIdentifier};
use crate::interface::{ComponentInterface, Error};
use askama::Template;
use std::borrow::Borrow;

use super::filters;
pub struct ErrorCodeType {
Expand Down
23 changes: 11 additions & 12 deletions uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::borrow::Borrow;
use std::collections::{HashMap, HashSet};
use std::fmt;

use anyhow::Result;
use askama::Template;
Expand Down Expand Up @@ -271,22 +271,22 @@ impl CodeOracle for KotlinCodeOracle {
}

/// Get the idiomatic Kotlin rendering of a class name (for enums, records, errors, etc).
fn class_name(&self, nm: &dyn fmt::Display) -> String {
fn class_name(&self, nm: &str) -> String {
nm.to_string().to_camel_case()
}

/// Get the idiomatic Kotlin rendering of a function name.
fn fn_name(&self, nm: &dyn fmt::Display) -> String {
fn fn_name(&self, nm: &str) -> String {
nm.to_string().to_mixed_case()
}

/// Get the idiomatic Kotlin rendering of a variable name.
fn var_name(&self, nm: &dyn fmt::Display) -> String {
fn var_name(&self, nm: &str) -> String {
nm.to_string().to_mixed_case()
}

/// Get the idiomatic Kotlin rendering of an individual enum variant.
fn enum_variant_name(&self, nm: &dyn fmt::Display) -> String {
fn enum_variant_name(&self, nm: &str) -> String {
nm.to_string().to_shouty_snake_case()
}

Expand All @@ -295,7 +295,7 @@ impl CodeOracle for KotlinCodeOracle {
/// This replaces "Error" at the end of the name with "Exception". Rust code typically uses
/// "Error" for any type of error but in the Java world, "Error" means a non-recoverable error
/// and is distinguished from an "Exception".
fn error_name(&self, nm: &dyn fmt::Display) -> String {
fn error_name(&self, nm: &str) -> String {
let name = nm.to_string();
match name.strip_suffix("Error") {
None => name,
Expand Down Expand Up @@ -328,7 +328,6 @@ impl CodeOracle for KotlinCodeOracle {

pub mod filters {
use super::*;
use std::fmt;

// This code is a bit unfortunate. We want to have a `KotlinCodeOracle` instance available for
// the filter functions, so that we don't always need to pass as an argument in the template
Expand Down Expand Up @@ -397,22 +396,22 @@ pub mod filters {
}

/// Get the idiomatic Kotlin rendering of a class name (for enums, records, errors, etc).
pub fn class_name(nm: &dyn fmt::Display) -> Result<String, askama::Error> {
pub fn class_name(nm: &str) -> Result<String, askama::Error> {
Ok(oracle().class_name(nm))
}

/// Get the idiomatic Kotlin rendering of a function name.
pub fn fn_name(nm: &dyn fmt::Display) -> Result<String, askama::Error> {
pub fn fn_name(nm: &str) -> Result<String, askama::Error> {
Ok(oracle().fn_name(nm))
}

/// Get the idiomatic Kotlin rendering of a variable name.
pub fn var_name(nm: &dyn fmt::Display) -> Result<String, askama::Error> {
pub fn var_name(nm: &str) -> Result<String, askama::Error> {
Ok(oracle().var_name(nm))
}

/// Get the idiomatic Kotlin rendering of an individual enum variant.
pub fn enum_variant(nm: &dyn fmt::Display) -> Result<String, askama::Error> {
pub fn enum_variant(nm: &str) -> Result<String, askama::Error> {
Ok(oracle().enum_variant_name(nm))
}

Expand All @@ -421,7 +420,7 @@ pub mod filters {
/// This replaces "Error" at the end of the name with "Exception". Rust code typically uses
/// "Error" for any type of error but in the Java world, "Error" means a non-recoverable error
/// and is distinguished from an "Exception".
pub fn exception_name(nm: &dyn fmt::Display) -> Result<String, askama::Error> {
pub fn exception_name(nm: &str) -> Result<String, askama::Error> {
Ok(oracle().error_name(nm))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public typealias {{ name }} = {{ builtin|type_name }}

{%- when Some with (config) %}
{%- let type_name=self.type_name(config) %}
{%- let ffi_type_name=self.builtin_ffi_type()|ffi_type_name %}
{%- let ffi_type_name=self.builtin_ffi_type().borrow()|ffi_type_name %}

{#- Create a typealias so that this type can be imported when used as an external type #}
public typealias {{ name }} = {{ type_name }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name }
{% if e.is_flat() %}
return when(buf.getInt()) {
{%- for variant in e.variants() %}
{{ loop.index }} -> {{ type_name }}.{{ variant.name()|exception_name }}({{ TypeIdentifier::String|read_fn }}(buf))
{{ loop.index }} -> {{ type_name }}.{{ variant.name()|exception_name }}({{ TypeIdentifier::String.borrow()|read_fn }}(buf))
{%- endfor %}
else -> throw RuntimeException("invalid error enum value, something is very wrong!!")
}
Expand Down
2 changes: 1 addition & 1 deletion uniffi_bindgen/src/bindings/kotlin/templates/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private inline fun <U, E: Exception> rustCallWithError(errorHandler: CallStatusE
// with the message. but if that code panics, then it just sends back
// an empty buffer.
if (status.error_buf.len > 0) {
throw InternalException({{ TypeIdentifier::String|lift_fn }}(status.error_buf))
throw InternalException({{ TypeIdentifier::String.borrow()|lift_fn }}(status.error_buf))
} else {
throw InternalException("Rust panic")
}
Expand Down
Loading

0 comments on commit 12fb0cd

Please sign in to comment.