我正在尝试使用Diesel和diesel_geography从使用Rust的PostGIS数据库读取数据。 下面是我得到的错误: error[E0277]: the trait bound `diesel_geography::types::GeogPoint: serde::Serialize` is not satisfied
--> src/models.rs:11:5
|
11 | pub coordinates: GeogPoint,
| ^^^ the trait `serde::Serialize` is not implemented f
可以声明一个表示特征的关联类型吗?如果不是,我能做什么呢?尝试做以下事情:
trait Foo {
/// A trait representing all types that can be returned from baz()
type ReturnType;
fn baz(&self) -> Self::ReturnType;
}
我遇到的最大问题是Sized特征,因为我需要一个函数来返回实现ReturnType的类型的项的向量
trait Foo {
type ReturnType;
// option 1
fn bar(
我目前正在尝试实现库中的一个特性(在我的例子中是serde),它期望返回一个特征(参见示例函数next),但我只有一个可利用的超特征,但我不知道如何将一个超特性降为子特征。
示例代码:
/// Imagine this type as a trait from a library, which i cannot change
trait SubTrait {
fn get_bool() -> bool;
}
trait SuperTrait: SubTrait {
fn get_string() -> String;
}
/// Imagine this typ
我是生锈特征的新手,所以这可能是因为我对超级特征、dyn或其他任何东西的误解。我试图在枚举中使用一个特性对象来:
在枚举的这个元素中可以使用的具体类型上加上一个特征。
确保枚举仍然可以派生Copy
最小的示例(无法在上编译相关错误)是:
#[derive(Copy)]
enum Foo {
A,
B(dyn MyTraitWhichIsCopy),
}
trait MyTraitWhichIsCopy: Copy {}
错误是:
error[E0204]: the trait `Copy` may not be implemented for this typ
我有一个顶级特征,它包含许多类和特征,比如:
trait Trees { self: Types =>
trait Tree
trait IdentifiedTree extends Tree
trait Empty extends Tree
/** The factory for [[TypeUse]] instances */
trait TypeUse extends Tree
/** AST tree to represent erroneous trees */
object BadTree extends IdentifiedTree
/**
我正在接收一个插入到Vec<T>中的JSON数组。我使用serde进行解析。我想将向量插入到数据库表中。JSON数组可以很好地解析。字段publication_time是时区,所以我使用my_date_format和serde提供的example对其进行解析。当我向结构添加(派生) Insertable时,cargo build会失败,并显示以下信息 error[E0277]: the trait bound `DateTime<Local>: diesel::Expression` is not satisfied
--> src/models.rs:5:2
当我在生锈柴油中定义jsonb时,diesel = { version = "1.4.7", features = ["postgres","serde_json"] }是这样定义的:
#[macro_use]
extern crate diesel;
use diesel::pg::types::sql_types::Jsonb;
use rocket::serde::Deserialize;
use rocket::serde::Serialize;
fn main() {
println!("hello")
}
我有一个广泛的基础特征。有些类型只关心其功能的子集,因此我添加了一个子特性,要求用户实现更小的函数集。
此代码失败:
trait Base<T> {
fn foo(arg: bool);
}
// Ext is a narrowing of Base requiring user to provide alternative, simpler interface for the same functionality
trait Ext<T>: Base<T> {
fn bar();
}
// implement Base<T>
我无法使用火箭和柴油从填充的mySQL数据库中检索datetime。
这是我的模型:
extern crate chrono;
use diesel::prelude::*;
use diesel::mysql::MysqlConnection;
use schema::chrisms;
use diesel::sql_types::Datetime;
use self::chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
#[derive(Serialize, Deserial
我正在柴油中发布一个原始sql。如果没有参数,代码就会编译。但是,当我使用bind向sql_query添加参数时,代码将不会编译。
pub fn find(session_id: Uuid) -> Result<Vec<Summary>, CustomError> {
let q = "select product, sum(price) as price from items where session_id = $1 group by product order by product";
let mut
我试过这个:
trait T {}
fn f() -> impl T {
unimplemented!();
}
fn main() {
f();
}
但是它给出了这个错误:
error[E0277]: the trait bound `!: T` is not satisfied
--> src/main.rs:3:11
|
3 | fn f() -> impl T {
| ^^^^^^ the trait `T` is not implemented for `!`
|
= note: the return typ
我目前正在尝试实现抽象功能,它将更新数据库中任何表的一些元字段,但在可识别方面遇到了问题。
我有一个数据库,其中每个表都有元字段:
....
pub updu: Option<Uuid>, // ID of a user who changed it
pub updt: Option<NaiveDateTime>, //updated with current date/time on every change
pub ver: Option<i32>, //Version increases on every change
.....
我想实现一个函数,
我开始和Rust合作建立一个新的图书馆。我正在尝试实现以下几种可能的方法。
接下来的是更多的期望表达式,而不是真正的语法。我试图表达这一点的所有方法要么不编译,要么在实现别名特征时不编译。
struct ConcreteType;
struct CommonType;
trait Handler<Rin, Rout = Rin>{
fn handle_event(&self, msg: &Rin);
}
// alias Handler with one of the types defined as a common case
trait Handle
我正在尝试编写一个泛型方法,该方法接受返回Serialize值或Arc<Serialize>值的函数。我的解决方案是创建一个特征,以便在需要时解开Arc,并生成对基础值的引用:
use serde::Serialize;
use std::sync::Arc;
pub trait Unwrapper {
type Inner: Serialize;
fn unwrap(&self) -> &Self::Inner;
}
impl<T> Unwrapper for T
where
T: Serialize,
{
这个特征定义编译得很好:
trait Works {
fn foo() -> Self;
}
然而,这确实导致了一个错误:
trait Errors {
fn foo() -> Option<Self>;
}
error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> src/lib.rs:6:5
|
6 | fn foo() -> Option<Self>;
| ^^^^^^^^^^^^
我正在尝试将serde_json::Map的JSON值序列化到SQLite数据库中。我希望在Map中使用多个数据类型,并将它们转换为适当的SQLite数据类型。
映射是在collect_values中创建的,并传递给write_record函数。new_db函数创建传递给write_record的rustqlite::Connection上下文
但是,当我试图从映射中插入值时,我会得到以下错误
error[E0277]: the trait bound `serde_json::value::Value: rusqlite::types::to_sql::ToSql` is not satis