3,630 questions
0
votes
0
answers
32
views
Returning general json data in a tauri specta app [duplicate]
I'm working on a tauri application, Fluster. I'm trying to add a method to parse general tabular data using polars, and then return that data as a vec of hashmaps.
I'm currently running into this ...
1
vote
1
answer
101
views
How do I specialize the implementation of a function or struct for a single type?
Let's say I have a function that knows how to poll a no-result future in a way specific to my environment:
/// Poll the supplied future in some special way until it becomes ready.
fn poll_until_ready&...
0
votes
0
answers
50
views
How to override traits error response structure in raml
There is a trait defined for error response which has all error codes but for few resources I want to update a separate error response for few error codes like 400,403. I defined the 400 error data ...
2
votes
1
answer
96
views
Rust display trait performs poorly compared to `serde::serialize`
I've implemented a Rust clone of bgpdump that displays MRT messages in the same format as bgpdump. To print the message, I implemented the Display trait and printed it to stdout. However, I'm ...
-3
votes
1
answer
97
views
Using generics together with async, cannot find method of struct
I am implementing a function in Rust that take a callback function as an argument. As far as I learned, I can use Fn trait bound to do that.
But now I want this callback function to be async, and I ...
0
votes
1
answer
71
views
Idiomatic solution to enforce trait bounds
So I have a struct WeatherStation which consists of a name: String and probability distribution function which should implement the rand::distr::Distribution trait. It look something like this:
struct ...
2
votes
1
answer
90
views
Function objects with arguments that extend a trait
I have the following code. The compiler complains about the last line. I am wondering why. The WrappedFunction case class takes a function object with trait arguments. When I try to construct a ...
0
votes
0
answers
62
views
Which trait bound should be specified to mean "any selectable table" in Diesel ORM?
When writing a function that should be able to operate on any Diesel selectable table, for example something like "is this table empty", what is the correct way to specify the trait bound?
I ...
0
votes
1
answer
79
views
How can I make a trait that allows conversion between all types implementing it?
I'm writing a library that deals with numerical attributes. For this I have created a trait for all the number primitives:
trait Sealed {}
#[allow(private_bounds, reason = "To seal trait Number&...
1
vote
1
answer
152
views
Why use Rust traits for "implementing shared behavior" if each object with the same trait still needs its own implementation written?
Reading the Rust book and struggling to understand why I should use traits over methods if I still need to write an implementation of the trait for each object that can have that trait.
Apart from the ...
1
vote
0
answers
57
views
How do I make traits share conflicting methods and properties in PHP
I have this trait
HasExtendedRoles.php
trait HasExtendedRoles
{
use HasRoles;
use HasExtendedPermissions {
HasExtendedPermissions::getPermissionClass insteadof HasRoles;
...
5
votes
2
answers
163
views
Trying to make bounded floating point types in rust, problem with From and Into
Here is the code, I'm trying to make bounded floating point numbers.
I do this with a Bounds trait, that must be implemented for the generic type B in the Bounded number struct.
I would like to be ...
0
votes
1
answer
88
views
How to write test for Trait
I have a Trait called HasDefault
its main purpose is to ensure that the class has a static field $default which is callable and returns the default instance of the class using the Trait
This is what ...
0
votes
0
answers
19
views
Using generic constants and AsRef in operator overloading [duplicate]
So, at the moment, I'm implementing a matrix. This
fn multiply<const P: usize, T>(&self, rhs: T) -> Matrix<R, P>
where
T: AsRef<Matrix<C, P>>,
is the signature of ...
1
vote
1
answer
1k
views
Axum the trait Handler<_, _, _> is not implemented for fn item [duplicate]
While implementing a handler function for an Axum endpoint, I ran accross the following error
--> qubic-rpc/src/lib.rs:90:38
|
90 | .route("/balances/{id}", get(routes::...
0
votes
1
answer
46
views
How to unify error types for Sender::send and JoinHandle::join?
The signatures for Sender::send and JoinHandle::join are, respectively:
pub fn send(&self, t: T) -> Result<(), SendError<T>>
pub fn join(self) -> Result<T, Box<dyn Any + ...
5
votes
1
answer
66
views
Overflow when checking trait bound on a custom Add implementation
I'm trying to make a zero cost abstraction library that adds units to values, to ensure computations homogeneity at compile time.
The core type is:
pub struct QuantifiedValue<T, U: Unit> {
...
0
votes
1
answer
46
views
Implementing std::ops::Add for arguments with disimilar types [closed]
I want to create types, and implementations of std::ops::Add such that:
add(Position, Displacement) -> Position
add(Displacement, Displacement) -> Displacement
The purpose is to make nonsense ...
0
votes
0
answers
76
views
Trait with blank constraint on lifetime
This code (derived from a more complex example) compiles [playground]:
fn func1<'a>(s: &'a str) where 'static: 'a { println!("{s}"); }
fn func2<'a>(s: &'a str) { func1(s)...
1
vote
1
answer
50
views
I have extended a type with a trait, why is my Vec<impl ThatTrait> not accepting a type that implements that trait? [duplicate]
I have a trait Input that adds a to_custom_bytes() method to Strings and u64s.
trait Input {
fn to_custom_bytes(&self) -> Vec<u8>;
}
impl Input for u64 {
fn to_custom_bytes(&...
0
votes
3
answers
161
views
Can you emulate rust's Fn trait using type classes?
In Rust, there is no type for functions, but rather an Fn trait that looks something like this:
trait Fn<A: Tuple, R> {
fn call(self, args: A) -> R;
}
Then the type of a function can be ...
1
vote
0
answers
40
views
Recursive traits for flattening multidimensional vectors
Suppose I we want to flatten vectors:
[0, 50, 100] -> [0, 50, 100]
[[0], [50, 50], 100] -> [0, 50, 50, 100]
[[[0]]] -> [0]
We can define a trait such as
pub trait FlattenVec<A> {
...
2
votes
1
answer
157
views
Why can't you implement an impl Trait type in rust
Say that I have the following trait:
trait SayHello {
fn say_hello(self) -> String;
}
Why can I then do this:
fn say_hello_twice(this: impl SayHello) -> String {
let said_hello = this....
0
votes
0
answers
46
views
Is there a way to provide a derived implementation for traits that have a specific associated type?
I'm trying to write a blanket implementation of a trait for types that are another trait with a specific associated type.
That is, it is fully permissible to do this:
use std::{marker::PhantomData, ...
0
votes
0
answers
48
views
How to create a Vec of instances of a struct using const generics, while still using methods depending on the value of the generic in Rust?
I am trying to learn some Rust by creating a neural network from scratch.
I started by defining a Matrix struct using const generics in order to check for matrix size at compile time when performing ...