These are chat archives for rust-lang/rust

31st
May 2018
Tembo
@Tembocs
May 31 2018 06:48
Hi, I'm trying to rewrite C++ program in Rust. Classes in C++ have getters and setters. Are there any reasons for this in Rust? Is accessing the fields directly idiomatic?
Krys
@Krysme
May 31 2018 07:26
The good side of wrapping data inside of getters and setters that I can come up with is the consistency of exposed APIs when you change the internal representations
Tembo
@Tembocs
May 31 2018 07:27
@Krysme, I get it.
Are there built-in facilities for handling signals in Rust?
Krys
@Krysme
May 31 2018 07:29
and I think it contradicts with the "zero-overhead abstraction" principle of C++, since pretty much the only way you hide the structure with getter is that you return a copied instance to the outside
Tembo
@Tembocs
May 31 2018 07:31
@Krysme, if I understand what you are saying is, unless a struct is part of the exposed API there is no good reason to use getters and setters, right?
Krys
@Krysme
May 31 2018 07:32
indeed
there might be some other reason you might want to do that
sorry, I didn't think it through
Tembo
@Tembocs
May 31 2018 07:35
Ok, like what?
Krys
@Krysme
May 31 2018 07:35
customized setters may limit the values that can be set
for instance set_age (int)
Tembo
@Tembocs
May 31 2018 07:36
Ok, you are right.
Krys
@Krysme
May 31 2018 07:36
if it goes greater than 200, throw an exception or somthing else you wanna do
but you could also use sub-typing to do that
like wrapping the int to type age
and limit the value in the constructor
Tembo
@Tembocs
May 31 2018 07:37
I like the sub-typing idea.
Krys
@Krysme
May 31 2018 07:37
or in rust, in the static function that creates a new instance
like Age::new (int) returns an Option<Age>
and your struct contains an Age instead of an int
Tembo
@Tembocs
May 31 2018 07:40
noted, thank you so much.
Krys
@Krysme
May 31 2018 07:56
n p
John Hughes
@jondo2010
May 31 2018 10:47

Hi, pretty new to Rust, trying to use the lyon_geom crate, and in particular the lyon_geom::Segment trait (https://docs.rs/lyon_geom/0.10.1/lyon_geom/trait.Segment.html)

I'd like to polymorphically iterate over a Vec of objects that impl Segment, but Segment has Sized bound in its definition, which I guess means I can't do this?

Can someone explain this to me? Am I going about this backwards? How am I supposed to do this?

Zakarum
@omni-viral
May 31 2018 10:55
@jondo2010 If you have segments: Vec<T> in context where T: Segment what can stop you from writing for segment in segments { ... }?
Sized doesn't limit you here
It only limits you from doing impl Segment for <some_possibly_unsized_type>
John Hughes
@jondo2010
May 31 2018 10:58
trait Segment: Copy + Sized {..}
let foo: Vec<Segment>> = vec![];
This doesn't work, rustc says it cannot be made into an object
Zakarum
@omni-viral
May 31 2018 10:58
Ah. That's true. You cannot have Segment as trait object
You need to use generic type
fn foo<T: Segment>() {
  let foo: Vec<T> = vec![];
}
Not sure why author desided to add Sized bound
John Hughes
@jondo2010
May 31 2018 11:00
So this basically prevents me from using it for runtime polymorphism?
Zakarum
@omni-viral
May 31 2018 11:01
Even if it wasn't Sized you can't write Vec<TraitName>
Because parameter for Vec must be Sized :/
And yes. You can't use it for runtime polymorphism
John Hughes
@jondo2010
May 31 2018 11:02
doh.
Zakarum
@omni-viral
May 31 2018 11:02
But you probably shouldn't anyway
For start it has associated type.
So to make trait object you need to write Segment<Scalar=X>
Even if Segment wasn't Sized
Why do you want runtime polymorphism? May I ask you to do compile-time polymorphism instead? :smile:
Bryan Burgers
@bryanburgers
May 31 2018 11:04
If you do need polymorphism, you can use Vec<Box<Segment>>. Then your vector can hold a box to any type of segment.
Zakarum
@omni-viral
May 31 2018 11:04
@bryanburgers No. Cause Sized
John Hughes
@jondo2010
May 31 2018 11:07
Right. So my use case is that I have input data that maps to various types that impl Segment, so my thinking was, at runtime build up my Vec<Segment<Scalar=f64>> and call methods from Segment
Zakarum
@omni-viral
May 31 2018 11:25

input data that maps to various types

Use enum

And implement Segment for it
Anonymous sum types would be great here. But we have no such thing in rust
John Hughes
@jondo2010
May 31 2018 11:26
And map the methods to enum member methods in a match?
Zakarum
@omni-viral
May 31 2018 11:26
Yeap
That's boilderplate-code :smile:
Alternatively you may ask author of lyon_geom to remove Sized bound
I can't see how it useful
Ah. There some methods return Self
But I prefer to add Sized bound directly to methods
Then you can use all other methods with trait-object
John Hughes
@jondo2010
May 31 2018 11:30
Got it! Thanks
@omni-viral, I've been following your work in gfx-rs. Are you still working on porting Amethyst to use gfx-hal?
Zakarum
@omni-viral
May 31 2018 11:34
@jondo2010 Yeah. I finished some work with xfg to add support of multiqueue execution (with help of gfx-chain) and compute support .
Now I'm thinking what else is left to do before integrating everything to amethyst
I have some time before hal is released
I plan to merge hal based rendering ASAP after hal release
Tembo
@Tembocs
May 31 2018 12:06
@xoac , thanks.
Sylwester Rąpała
@xoac
May 31 2018 12:09
I know that my string always will have a 42 character long (ASCII) how should I implement my struct? I would like also add some checking at constructor that is it valid etc.
And I would also want to interact with it as a str
struct FixedString {
    str: [u8; 42]
}
Zakarum
@omni-viral
May 31 2018 12:11
You can do all of this with unsafe code
Sylwester Rąpała
@xoac
May 31 2018 12:11
there is no ready solution?
Zakarum
@omni-viral
May 31 2018 12:12
Not sure. But it looks like not much to code
Just make constructing method, assert there. Implement deref to str
Denis Lisov
@tanriol
May 31 2018 13:33
@xoac Depending on the requirements, I'd suggest storing a String or, if this code is likely performance-critical, would look at ArrayString or something like that.
Farzeen
@thefzsalam
May 31 2018 15:53
How to annotate a module that I only use inside #[cfg(test)] mod test { .. }, so that warnings like this: warning: struct is never used: FooStruct are not shown during regular builds?
It is a library project. I am doing mod foo_test_helpers; from lib.rs. FooStruct is in foo_test_helpers module.
red75prime
@red75prime
May 31 2018 16:19
#[cfg(test] mod foo_test_helpers; ?
Farzeen
@thefzsalam
May 31 2018 16:25
thanks @red75prime
Dylan DPC
@Dylan-DPC
May 31 2018 17:07
?
Avi Srivastava
@k3yavi
May 31 2018 17:09
Recommendations for memory-frugal hashMap ?
I liked the sparsepp (C++), but I found the default hashMap in Rust (collections) taking a lot of memory for my use-case
Farzeen
@thefzsalam
May 31 2018 17:19
Sorry, kids were messing up with the keyboard :confused:
Dylan DPC
@Dylan-DPC
May 31 2018 17:22
:joy: