Questions tagged [trait-bounds]
6 questions
3
votes
2 answers
Why use trait bounds in struct definitions with generic type parameters?
I can define a struct type that uses a generic type parameter with a trait bound:
struct MyStruct {
field: T,
}
This prevents me me from instantiating MyStruct with a generic type which does not meet the trait bound:
// Note: does not…

RBF06
- 2,013
- 2
- 21
- 20
1
vote
0 answers
How to accept callback as method parameter where the callback's parameter has a trait bound
I'm trying to design an API that enables zero-copy processing of deserialized data. My plan is to pass a callback into the struct that's actually doing the buffering data from the network then pass the deserialized struct containing data borrowed…

UserSpaceMan
- 31
- 5
0
votes
1 answer
Rust generic function accepting both primitive and rug integers
I'm trying to write a generic function that operates on integers, which can accept either primitive integers or multiprecision ones (via rug). The tricky bit is that references to rug Integers have lazy arithmetic operations, that return incomplete…

KSV
- 31
- 4
0
votes
1 answer
Enforce calling one closure before another
I want to bundle two closures together in a struct. One closure is there to test if the inputs are valid, and the other closure is to actually do the stuff. Here is one variation that works:
struct Foo
{
test: T,
func: F,
}
fn…

Arthur
- 653
- 2
- 6
- 21
0
votes
1 answer
How to express the trait bounds of a generic method when implementing a trait that requires this method to exist?
General Question
I do have a struct that requires a type constraint on a generic method in its impl block.
When implementing a foreign trait, I would like to rely on the presence of said method.
Is there some workaround in Rust, that allows to…

soulsource
- 197
- 7
0
votes
1 answer
E0277: Is there a way to resolve trait bound ambiguity in use of AsRef with fixed-size array type? (e.g. `impl AsRef<[u8; 3]>`)
I'm trying to make the impl AsRef<[u8; 3]> to work in function parameters.
Playgrounds repro: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e99007b0571ed2f088b3e38a6692ccdf
Here is the MPE:
fn print_bytes_3(bytes: impl…

Preco Plusb
- 615
- 1
- 9
- 14