Here's a minimal example representing the type of problem I'm running into.
use core::fmt::Debug;
pub trait Config {
type A: Debug;
}
#[derive(Debug)]
pub struct Info<T: Config> {
pub field: T::A,
}
pub struct Conf;
impl Config for Conf {
type A = i128;
}
fn main() {
let s = Info::<Conf> {
field: 123
};
dbg!(s);
}
The framework that I'm using (Substrate) uses this concept of a Config trait that aggregates all generic types for a module (pallet).
The problem is that trying to #[derive(Debug)] for a struct that only uses the associated types of the object T implementing Config still requires that T implements Debug itself.
error[E0277]: `Conf` doesn't implement `Debug`
--> src/main.rs:22:5
|
22 | dbg!(s);
| ^^^^^^^ `Conf` cannot be formatted using `{:?}`
|
= help: the trait `Debug` is not implemented for `Conf`
= note: add `#[derive(Debug)]` to `Conf` or manually `impl Debug for Conf`
Moreover, I don't have control of the implementation of the Conf object. Regardless, I'm not trying to print anything about the Conf object itself.
Is there a way to make #[derive(Debug)] for Info ignore T?
Unfortunately, not as of today. You have to impl Debug manually:
impl<T: Config> fmt::Debug for Info<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Info").field("field", &self.field).finish()
}
}
There is an intent to make it possible to create what is called "perfect derive": derive bounds based on what needed and not the generic parameters. See for example this lang team design meeting proposal. But for now there is nothing.
Related
I'm working on a network service that's intended to work with either a TcpStream or stdin/stdout. I get a compile error: the trait tokio::io::util::async_read_ext::AsyncReadExt cannot be made into an object. Currently my workaround is to use a wrapper enum:
enum ClientReader {
Stream(OwnedReadHalf),
Stdin(Stdin),
}
enum ClientWriter {
Stream(OwnedWriteHalf),
Stdout(Stdout),
}
This requires match blocks all over the place, which seems inelegant.
I made a simplified project to repro the issue:
Cargo.toml
[package]
name = "demo"
version = "0.1.0"
authors = ["test"]
edition = "2018"
[dependencies]
tokio = { version = "0.2", features = ["full"] }
src/main.rs
use tokio::io::AsyncReadExt;
struct Test {
test: Box<dyn AsyncReadExt>,
}
fn main () {}
This produces a similar error:
error[E0038]: the trait `tokio::io::AsyncReadExt` cannot be made into an object
--> src/main.rs:4:3
|
4 | test: Box<dyn AsyncReadExt>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `tokio::io::AsyncReadExt` cannot be made into an object
|
::: /home/???/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.22/src/io/util/async_read_ext.rs:162:12
|
162 | fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
| ---- the trait cannot be made into an object because method `read` references the `Self` type in its return type
...
280 | fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>
| ---------- the trait cannot be made into an object because method `read_exact` references the `Self` type in its return type
I'm not sure how to proceed. I was considering a giant impl block for the enum wrapper, but that seems like more work than the match blocks. In an OO language there'd be a parent class or interface so I investigated the trait_enum crate to automate making a wrapper impl but had a lot of trouble getting that to work.
At the moment the only cleanup I'm sure will work is to move the workaround into a macro or function.
I'd appreciate any feedback on a better way to do this. :)
EDIT: per suggestion by user4815162342 I made the struct generic over type AsyncReadExt and this appears to work for my example. Will try on my larger project later.
use tokio::io::AsyncReadExt;
struct Test<T: AsyncReadExt> {
test: T,
}
async fn myfn<T: AsyncReadExt>(mut t: Test<T>) where T: std::marker::Unpin {
let mut v = Vec::<u8>::new();
t.test.read_buf(&mut v).await;
}
fn main () {}
To turn an AsyncRead into a trait object, you should use the type Pin<Box<dyn AsyncRead>>.
use std::pin::Pin;
use tokio::io::AsyncRead;
struct Test {
test: Pin<Box<dyn AsyncRead>>,
}
impl Test {
fn new<T: AsyncRead>(io: T) -> Self {
Self {
test: Box::pin(io),
}
}
}
The AsyncReadExt trait is an extension trait, and you should always use the AsyncRead trait when mentioning the type of an AsyncRead.
I am using newtypes like struct GuildId(i64); for the columns in my diesel model structs. Currently I am implementing these traits:
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
pub struct $name(pub i64);
impl AsExpression<BigInt> for $name { /* delegate to <i64 as AsExpression<BigInt> */ */ }
impl<ST, DB: Backend> Queryable<ST, DB> for $name
where i64: FromSql<ST, DB> { /* also delegate to i64 */
However, when I try to use this type in the following model structs:
#[derive(Associations, Identifiable, Queryable)]
#[belongs_to(Guild)]
struct Channel {
guild_id: GuildId,
// other fields
}
#[derive(Identifiable, Queryable)]
struct Guild {
id: GuildId,
// other fields
}
Channel still does not implement BelongingToDsl. When I try to cast it to the trait, it fails to compile with the following message:
error[E0277]: the trait bound `diesel::query_builder::select_statement::SelectStatement<webcord_schema::schema::channels::table>: diesel::query_dsl::filter_dsl::FilterDsl<diesel::expression::operators::Eq<webcord_schema::schema::channels::columns::guild_id, &webcord_schema::models::GuildId>>` is not satisfied
--> src/index/guild.rs:23:32
|
23 | let channels = <models::Channel as BelongingToDsl<&models::Guild>>::belonging_to(&guild)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::query_dsl::filter_dsl::FilterDsl<diesel::expression::operators::Eq<webcord_schema::schema::channels::columns::guild_id, &webcord_schema::models::GuildId>>` is not implemented for `diesel::query_builder::select_statement::SelectStatement<webcord_schema::schema::channels::table>`
|
= help: the following implementations were found:
<diesel::query_builder::select_statement::SelectStatement<F, S, D, W, O, L, Of, G, LC> as diesel::query_dsl::filter_dsl::FilterDsl<Predicate>>
= note: required because of the requirements on the impl of `diesel::query_dsl::filter_dsl::FilterDsl<diesel::expression::operators::Eq<webcord_schema::schema::channels::columns::guild_id, &webcord_schema::models::GuildId>>` for `webcord_schema::schema::channels::table`
error[E0277]: the trait bound `webcord_schema::models::GuildId: diesel::expression::Expression` is not satisfied
--> src/index/guild.rs:23:32
|
23 | let channels = <models::Channel as BelongingToDsl<&models::Guild>>::belonging_to(&guild)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::expression::Expression` is not implemented for `webcord_schema::models::GuildId`
|
= note: required because of the requirements on the impl of `diesel::expression::Expression` for `&webcord_schema::models::GuildId`
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::BigInt>` for `&webcord_schema::models::GuildId`
= note: required because of the requirements on the impl of `diesel::query_dsl::belonging_to_dsl::BelongingToDsl<&webcord_schema::models::Guild>` for `webcord_schema::models::Channel`
What traits am I missing?
That error is not related to BelongingToDsl, but to the incomplete implementation of the custom new type wrapper.
As the error message indicates you are missing a trait impl for your new type wrapper:
error[E0277]: the trait bound `webcord_schema::models::GuildId: diesel::expression::Expression` is not satisfied
--> src/index/guild.rs:23:32
|
23 | let channels = <models::Channel as BelongingToDsl<&models::Guild>>::belonging_to(&guild)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::expression::Expression` is not implemented for `webcord_schema::models::GuildId`
|
= note: required because of the requirements on the impl of `diesel::expression::Expression` for `&webcord_schema::models::GuildId`
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::BigInt>` for `&webcord_schema::models::GuildId`
= note: required because of the requirements on the impl of `diesel::query_dsl::belonging_to_dsl::BelongingToDsl<&webcord_schema::models::Guild>` for `webcord_schema::models::Channel`
The interesting line is the second line in = note: required because of the requirements on the impl of diesel::expression::AsExpression<diesel::sql_types::BigInt> for '&webcord_schema::models::GuildId. This means you need to add at least an AsExpression<_> impl for a reference to your new type wrapper.
So now general speaking: There is this test case showing how to implement custom types in general. You will see that the custom type on rust side uses two custom derives (AsExpression and FromSqlRow) that are basically implementing the traits you've already implemented manually and additionally the missing ones. Additionally a ToSql/FromSql impl is required to describe how the type should be translated into/from a sql type.
Summing that up your type definition should probably look like:
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, AsExpression, FromSqlRow)]
#[sql_type = "diesel::sql_types::BigInt")]
pub struct $name(pub i64);
impl<DB> ToSql<diesel::sql_types::BigInt, DB> for $name
where DB: Backend,
i64: ToSql<diesel::sql_types::BigInt, DB>,
{
fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
<i64 as ToSql<diesel::sql_types::BigInt, DB>>::to_sql(&self.0, out)
}
}
impl<DB> FromSql<diesel::sql_types::BigInt, DB> for $name
where DB: Backend,
i64: FromSql<diesel::sql_types::BigInt, DB>
{
fn from_sql(bytes: Option<&DB::RawValue>) -> deserialize::Result<Self> {
<i64 as FromSql<diesel::sql_types::BigInt, DB>>::from_sql(bytes).map($name)
}
}
I want to print the instance of Tweet datatype in main function, but the summary trait don't implement the debug trait. Is there any way to implement a trait on trait or any work around.
uncommentating the second line and commentating the first line would work because String type implements the Display trait.
#[derive(Debug)]
struct Tweet {
name: String,
}
pub trait Summary {
fn summarize(&self) -> String;
}
impl Summary for Tweet {
fn summarize(&self) -> String {
format!("#{}", &self.name)
}
}
fn summarizeable(x: String) -> impl Summary {
Tweet { name: x }
}
fn main() {
//1.
println!("{:#?}", summarizeable(String::from("Alex")));
//2.println!("{}",summarizeable(String::from("Alex")).summarize());
}
error[E0277]: impl Summary doesn't implement std::fmt::Debug -->
src/main.rs:26:29 | 26 | /1./
println!("{:#?}",summarizeable(String::from("Alex"))); |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
impl Summary cannot be
formatted using {:?} because it doesn't implement std::fmt::Debug | = help: the trait std::fmt::Debug is not implemented for impl Summary = note: required by std::fmt::Debug::fmt
error: aborting due to previous error
For more information about this error, try rustc --explain E0277.
error: Could not compile p1.
To learn more, run the command again with --verbose.
You can require that anything that impls Summary must also impl std::fmt::Debug as follows:
pub trait Summary : std::fmt::Debug { // Summary requires Debug
fn summarize(&self) -> String;
}
If you do not want to tie Debug to Summary you can always introduce another trait subsuming the other two:
pub trait DebuggableSummary : Summary + std::fmt::Display {}
I would like to implement Borrow for UserFriendlyDataStructure to provide access to the internal_data field within a function that should be agnostic with respect to the data provider. The type of the internal_data field is determined by the type associated to trait TraitA. Note that the Sealed trait ensures that none of these traits here can be implemented by other crates; this is functionality that strictly I provide. Furthermore, the type TraitA::Data is restricted by the empty trait DataTrait to prevent UserFriendlyDataStructure from being used as that type.
The following example explains best:
use std::borrow::Borrow;
use std::marker::PhantomData;
mod private {
pub trait Sealed {}
}
pub trait DataTrait: private::Sealed {}
pub trait TraitA: private::Sealed {
type Data: DataTrait;
}
pub struct UserFriendlyDataStructure<A: TraitA> {
internal_data: A::Data,
_a: PhantomData<A>,
}
impl<A: TraitA> Borrow<A::Data> for UserFriendlyDataStructure<A> {
fn borrow(&self) -> &A::Data {
&self.internal_data
}
}
pub fn important_function<A: TraitA, T: Borrow<A::Data>>(data: &T) {
let _internal_data = data.borrow();
// Do lots of work.
}
#[cfg(test)]
mod tests {
use super::*;
pub struct TestData(u32);
impl super::private::Sealed for TestData {}
impl DataTrait for TestData {}
pub struct TestProvider;
impl super::private::Sealed for TestProvider {}
impl TraitA for TestProvider {
type Data = TestData;
}
#[test]
fn basic_test() {
let ufds: UserFriendlyDataStructure<TestProvider> = UserFriendlyDataStructure {
internal_data: TestData(100),
_a: PhantomData::default(),
};
important_function::<TestProvider, _>(&ufds);
}
}
Unfortunately, the compiler complains:
error[E0119]: conflicting implementations of trait `std::borrow::Borrow<UserFriendlyDataStructure<_>>` for type `UserFriendlyDataStructure<_>`:
--> src/lib.rs:19:1
|
19 | impl<A: TraitA> Borrow<A::Data> for UserFriendlyDataStructure<A> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<T> std::borrow::Borrow<T> for T
where T: ?Sized;
Is there a way to achieve what I am trying to do?
The compiler can be cajoled into accepting the code by introducing a redundant second type parameter that is constrained to be identical to A::Data:
impl<A, D> Borrow<D> for UserFriendlyDataStructure<A>
where
A: TraitA<Data = D>,
D: DataTrait,
{
fn borrow(&self) -> &A::Data {
&self.internal_data
}
}
I don't know why this works, and simply constraining A::Data: DataTrait doesn't. I think the compiler should accept both versions.
(Full code on the playground)
Edit: The fact that we need the redundant type D in the above code appears to be a shortcoming of the current compiler implementation, and is hopefully resolved once the experimental type inference engine chalk gets integrated in the compiler.
Suppose I want to create some type that wraps some other generic type, like so:
struct MyWrapper<T> {
pub inner: T,
}
Now I want my type to have a method if the inner type satisfies a specific bound. For example: I want to print it (in this example without using fmt traits for simplicity). To do this I have two possibilities: adding a bound to the impl or to the method itself.
Method Bound
impl<T> MyWrapper<T> {
pub fn print_inner(&self) where T: std::fmt::Display {
println!("[[ {} ]]", self.inner);
}
}
When calling this function with a MyWrapper<()> I get:
error[E0277]: `()` doesn't implement `std::fmt::Display`
--> src/main.rs:20:7
|
20 | w.print_inner();
| ^^^^^^^^^^^ `()` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string
|
= help: the trait `std::fmt::Display` is not implemented for `()`
Impl Bound
impl<T: std::fmt::Display> MyWrapper<T> {
pub fn print_inner(&self) {
println!("[[ {} ]]", self.inner);
}
}
Calling it incorrectly again, gives:
error[E0599]: no method named `print_inner` found for type `MyWrapper<()>` in the current scope
--> src/main.rs:19:7
|
1 | struct MyWrapper<T> {
| ------------------- method `print_inner` not found for this
...
19 | w.print_inner();
| ^^^^^^^^^^^
|
= note: the method `print_inner` exists but the following trait bounds were not satisfied:
`() : std::fmt::Display`
My question is: what is more idiomatic? Are there semantic differences (aside from lifetime stuff with traits, explained here)? Are there differences apart from the compiler message?
One semantic difference is that with the type bound on the method you can partially implement a trait:
trait Trait {
fn f(self) where Self: std::fmt::Display;
fn g(self);
}
struct Struct<T>(T);
impl<T> Trait for Struct<T> {
fn f(self) where Struct<T>: std::fmt::Display {
println!("{}", self);
}
fn g(self) {
println!("Hello world!");
}
}
fn main() {
let s = Struct(vec![1]);
// f is not implemented, but g is
//s.f();
s.g();
}
This may be useful if you have many optional methods with different type bounds, which would otherwise require separate traits.