Questions tagged [traits]
In computer programming, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs"
traits
3,573
questions
0
votes
0
answers
66
views
Is there a way to limit a Impl block to implement another trait?
I have a trait
pub trait LocationProvider<T>
where T: Into<DateTime<Utc>>
{
fn get_location(&self, time: T) -> Option<Location>;
fn get_locations(&self,...
1
vote
1
answer
54
views
How can I generalize some bit twiddling operations to all "unsigned" primitive types?
I'm trying to learn something about number traits and want to write some flexible functions for bit-twiddling. This problem fell out of it:
I have defined a few functions that take arguments width and ...
0
votes
1
answer
68
views
Kotlin scope functions in Rust?
Studying Rust and Kotlin at the moment, I wonder whether something as neat as Kotlin's Scope functions (let, apply, run, etc.) could be / are implemented in Rust?
-1
votes
2
answers
42
views
How to implement the `From` trait for a generic type, and retrieve the internal generic type?
For example I have below generic struct:
use std::convert::From;
pub struct Rect<T> {
pub top_left: (T, T), // 0 is x, 1 is y.
pub bottom_right: (T, T), // 0 is x, 1 is y.
}
impl<T> ...
1
vote
1
answer
39
views
Scala 2 to Scala 3: Instantiating a trait no longer recognises new methods
The following code compiles and runs in Scala 2:
trait SomeTrait {
val myName: String
}
class SomeClass() extends SomeTrait {
override val myName: String = "Billy Banana"
}
val ...
0
votes
1
answer
48
views
Understanding sealed traits in Rust
I am trying to create a mock object for testing in my Rust project. I am having an issue where I would like to implement a generic trait the type parameter of which must also implement another trait. ...
-1
votes
0
answers
29
views
Unsatisfied trait bounds when using lib externally
The Code
AsyncWebsocketClient
I'm currently working on an AsyncWebsocketClient which is using tokio_tungstenite internally, respectively futures::Sink<tokio_tungstenite::Message, Error = ...
-2
votes
0
answers
49
views
Unsatisfied trait bound error despite being implemented
I'm trying to implement a trait WebsocketIO for T but it always says the trait bounds were not satisfied when I'm trying to use it on my AsyncWebsocketClient. AsyncWebsocketClient is supposed to work ...
-2
votes
0
answers
191
views
Why do I get an error " ERROR: Failed building wheel for traits Failed to build traits"?
I am coding with python and need to install the module "fitz", but get errors.
I installed wheels, tried the cache solution "pip install --no-cache-dir", but i still get the error ...
0
votes
0
answers
31
views
Complex trait compiles when using local trait but not crate.io one
I published a crate on crates.io recently and when I tried using it my code no longer compiles, reverting to the local version through crate = { path = "/path/to/crate/" } fixes compilation. ...
0
votes
1
answer
92
views
Multiple inheritance in Rust
I'm just starting in the Rust (come from python and C#) world and I know this question has been asked many times, but I'm struggling to understand the answers as they are very generic (Foo, Bar, Baz). ...
1
vote
1
answer
83
views
Why must T be 'static when cloning into Rc<RefCell<dyn Trait>>?
Here is my code; in Ref::computed I get an error that F and U must be 'static:
use std::cell::RefCell;
use std::rc::Rc;
trait Observer<T> {
fn update(&mut self, v: &T);
}
struct ...
0
votes
1
answer
73
views
Why are trait objects usually used via references (&dyn Trait) or smart Pointers (like Box<dyn Trait>)?
In Rust, why are trait objects usually used via references (&dyn Trait) or smart Pointers (like Box<dyn Trait>)? Does it have to be? Or is it better to use it this way?
1
vote
2
answers
103
views
How to unsafely get a concrete reference from a Box<dyn Trait>?
I want to reference the specific struct in a Box<dyn Trait>. I know exactly what the struct is so I am ok with using unsafe operations. When I typecast it using raw pointers it gives a SIGSEGV.
...
0
votes
1
answer
155
views
Understanding `impl dyn Trait`
I can't wrap my head around the second impl block. In my understanding, impl is typically used to implement a trait/methods on a concrete type like a struct. However, what does it mean to implement ...