Skip to main content

Questions tagged [traits]

In computer programming, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs"

1 vote
1 answer
49 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 ...
exocortex's user avatar
  • 471
0 votes
1 answer
63 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?
NoBullsh1t's user avatar
-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> ...
linrongbin's user avatar
  • 3,235
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 ...
Joe PP's user avatar
  • 83
0 votes
1 answer
45 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. ...
m.tracey's user avatar
  • 313
-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 = ...
LimpidCrypto's user avatar
-2 votes
0 answers
48 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 ...
LimpidCrypto's user avatar
-2 votes
0 answers
165 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 ...
Doroteya Nikolova's user avatar
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. ...
Makogan's user avatar
  • 9,191
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). ...
Oliver Mohr Bonometti's user avatar
1 vote
1 answer
82 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 ...
Atarsei's user avatar
  • 13
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?
sundegan's user avatar
1 vote
2 answers
100 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. ...
twitu's user avatar
  • 615
0 votes
1 answer
137 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 ...
Code learner's user avatar
1 vote
1 answer
73 views

In Rust, Why does generics needs to specify lifetime as `'static` when passed as an argument?

struct TestStruct<F> { param: F, } trait TestTrait {} impl<F> TestTrait for TestStruct<F> {} fn t<F>(input: F) -> Box<dyn TestTrait> { Box::new(TestStruct { ...
ogios's user avatar
  • 45

15 30 50 per page
1
2 3 4 5
239