I have a little bit of confusion over 1-tuples. For example, in Rust I might have a 1-tuple defined like so:
struct PrimitiveOneTuple(String);
I might want to use this type of 1-tuple in a Rust function like so:
#[ic_cdk_macros::query]
#[candid::candid_method(query)]
async fn primitive_one_tuple_return_type() -> PrimitiveOneTuple {
// function body
}
}
The candid generated for the above looks like this:
primitive_one_tuple_return_type : () -> (text) query;
The candid that is generated from this turns PrimitiveOneTuple
into just text
. Is this correct behavior? Is there no such thing as a 1-tuple in Candid?
I’m not sure if it doesn’t exist but it can be omitted according to @chenyan and based on my own experience.
opened 02:42PM - 12 Jun 22 UTC
closed 04:31AM - 14 Jun 22 UTC
Below is a minimal example of my attempt to derive `CandidType` on a struct wher… e a field contains a value whose type originates in an external crate.
I assumed this would work but I get an error which says `the trait bound Foo: CandidType is not satisfied`. Is this expected?
```rust
#[derive(CandidType, Deserialize)]
pub struct MyStruct {
pub foos: BTreeSet<external_crate::Foo>,
}
```
The enum is defined as follows in the external crate. I've enabled the `serde1` feature in my `Cargo.toml` file.
```rust
const SIZE_OF_BAR: usize = 20;
```
```rust
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub enum Foo {
Bar([u8; SIZE_OF_BAR]),
}
```
Is the issue that `CandidType` can't be derived because of the `enum`?
I would prefer or hope that a 1-tuple would just be treated like a 1-tuple, but for example in Candid and the thus the JS agent, the 1-tuple is removed and just the underlying type is used.
chenyan
October 27, 2022, 11:18pm
4
I think the following would produce 1-tuple:
struct PrimitiveOneTuple((String,));
struct A(String)
is a newtype in Rust, and by convention, A
represents its inner type String
.
2 Likes
Ah, I will look into this. Seems like it’s on our side then.
Seems to be working so far, our Candid looks good and our tests are passing. Thanks!