These are chat archives for rust-lang/rust

8th
Nov 2017
Urjit Singh Bhatia
@urjitbhatia
Nov 08 2017 04:26
hi! is there a way to derive traits like PartialEq from fields of a struct?
Example:
struct Foo {
bar: Bar,
}
struct Bar{
val: <some val>
}
Ryan
@rnleach
Nov 08 2017 04:26
Sure...
Urjit Singh Bhatia
@urjitbhatia
Nov 08 2017 04:27
and I have traits added on Bar but I want to ignore rest of the fields on Foo
can I sort of pull them up into Foo?
Ryan
@rnleach
Nov 08 2017 04:28
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
struct Foo {
    bar: Bar,
}
Urjit Singh Bhatia
@urjitbhatia
Nov 08 2017 04:28
Here is a better example:
pub struct Spoke {
    bst: BoundingSpokeTime, // use this to derive PartialEq
    job_list: BinaryHeap<Job>, // Ignore this for the purpose of PartialEq, Ord etc
}
@rnleach I should have clarified earlier, I also want to ignore some fields
In this case ^, BoundingSpokeTime has a PartialEq implementation, I want Spoke to just use that as it is while ignoring job_list for any ordering or equality purposes
Ryan
@rnleach
Nov 08 2017 04:30
Ahh, I see. I don't think so. For something like that you would need just do a regular
 impl PartialOrd for Foo {
    ....
}
Urjit Singh Bhatia
@urjitbhatia
Nov 08 2017 04:31
got it - yeah I thought so. Wanted to check if someone knew a better way
thanks!
Ryan
@rnleach
Nov 08 2017 04:31
Someone else might, but sorry, I don't.
Urjit Singh Bhatia
@urjitbhatia
Nov 08 2017 04:34
:thumbsup: no worries - I’ll do a bit of copy+pasta for now
Moggers
@Moggers
Nov 08 2017 05:56
I have an entity trait which represents anything retrieved and put to my data layer
is there some way I can have it + Default without violating trait object constraints of not having a receivers?
Jonas Platte
@jplatte
Nov 08 2017 07:14
@Moggers No, that's not possible. Why do you want a Default constraint?
Moggers
@Moggers
Nov 08 2017 07:15
its no biggie, I just wanted to guarantee that all my db objects could be default()ed for simplcity but its really not necessary
Alexander Ivanov
@alehander42
Nov 08 2017 09:39
does somebody use syn library ?
Moggers
@Moggers
Nov 08 2017 10:53
where's the error_! macro from?
xhallix
@xhallix
Nov 08 2017 13:47

I'm using rust xml and want to return an xml parse

fn get_file_parser() -> xml::EventReader<_> {
    let file = File::open("/Users/me/dev/rust/foobar/examples/books.xml").unwrap();
    let file = BufReader::new(file);
    EventReader::new(file)
}
`

Here I'm getting

   |
20 | fn get_file_parser() -> xml::EventReader<_> {
   |                                          ^ not allowed in type signatures
How can I solve this?

I also tried

fn get_file_parser<T: std::io::Read>() -> xml::EventReader<T> {

which does not work

Denis Lisov
@tanriol
Nov 08 2017 13:59
The second one works for me...
Jonas Platte
@jplatte
Nov 08 2017 14:17
Huh? With that method body a generic signature shouldn't work..
@xhallix You need to either write out the exact type or use impl Trait (which is still unstable AFAIK)
Denis Lisov
@tanriol
Nov 08 2017 14:19
Oops, didn't check the body.
Jonas Platte
@jplatte
Nov 08 2017 14:19
The return type would be xml::EventReader<impl std::io::Read> with that
xhallix
@xhallix
Nov 08 2017 14:23
@jplatte how would that look like when using the exact type?
@jplatte I want to avoid anything unstable in the crate if possible
Jonas Platte
@jplatte
Nov 08 2017 14:23
The compiler should tell you..
When you use a wrong type as the return (e.g. if you just remove the -> xml::EventReader<_> completely
xhallix
@xhallix
Nov 08 2017 14:23
I tells me to use xml::EventReader<_>
Jonas Platte
@jplatte
Nov 08 2017 14:24
really?
xhallix
@xhallix
Nov 08 2017 14:24
yep
Jonas Platte
@jplatte
Nov 08 2017 14:24
What's the full error message?
xhallix
@xhallix
Nov 08 2017 14:24
mom
error[E0308]: mismatched types
  --> src/lib.rs:23:5
   |
23 |     EventReader::new(file)
   |     ^^^^^^^^^^^^^^^^^^^^^^ expected (), found struct `xml::EventReader`
   |
   = note: expected type `()`
              found type `xml::EventReader<std::io::BufReader<std::fs::File>>`
help: try adding a semicolon
   |
23 |     EventReader::new(file);
   |                           ^
help: try adding a return type
   |
20 | fn get_file_parser<T: std::io::Read>() -> xml::EventReader<_> {
   |                                        ^^^^^^^^^^^^^^^^^^^^^^
Jonas Platte
@jplatte
Nov 08 2017 14:25
If the error message really doesn't contain the required information, I'd try xml::EventReader<()> next to see if that returns a more useful error message
Ah but it already tells you the correct type!
xhallix
@xhallix
Nov 08 2017 14:25
yep but _ is not allowed in that case
Jonas Platte
@jplatte
Nov 08 2017 14:25
found type `xml::EventReader<std::io::BufReader<std::fs::File>>`
xhallix
@xhallix
Nov 08 2017 14:26
so I
aw man-.-
I jsut added <T>
thanks!
that 's it
Jonas Platte
@jplatte
Nov 08 2017 14:27
But the second help message is kind of weird, I'll check if there is a bug report for that...
@xhallix Couldn't (immediately) find an existing bug report. Could you open one with your code (just that one function + maybe your imports) and that error message? You can report issues here: https://github.com/rust-lang/rust/issues/new
xhallix
@xhallix
Nov 08 2017 14:34

Hm would this really be a bug ?
the second error message just gives me a suggestions

I think that I just didn't checked what the compiler wanted me to tell - so I think it is more a fault of mine

@jplatte What would you expect to see from the compiler in that case?
the suggestion should be something like
 fn get_file_parser<T: std::io::Read>() -> xml::EventReader<std::io::BufReader<std::fs::File>> {
?
Jonas Platte
@jplatte
Nov 08 2017 14:46
Potentially. Or the suggestion shouldn't be there in this case at all, since there was another suggestion already. (even though in this case not what you wanted)
But the compiler should never suggest changing the return type to something that contains a placeholder IMO, because that just never works
xhallix
@xhallix
Nov 08 2017 14:47
Okay I'll raise an issue for that
still-dreaming-1
@still-dreaming-1
Nov 08 2017 17:27
Does rust have named parameters?
Ryan
@rnleach
Nov 08 2017 17:27
Nope.
still-dreaming-1
@still-dreaming-1
Nov 08 2017 17:27
I guess it would be called named arguments...
Ryan
@rnleach
Nov 08 2017 17:28
No it does not, I've seen them requested a few times on the user forum, but I don't know about any RFC's at this time, let me check...
Here's a link to the discussion on the internals forum. July and August is when it was last active. It doesn't look like this will land anytime soon.
still-dreaming-1
@still-dreaming-1
Nov 08 2017 17:37
Ok thanks. Trying to decide which language will make us most productive and allow us to write the cleanest code that has the least number of errors. Strangely enough I'm thinking possibly Java just because the refactoring support in the JetBrains IDE is so amazing. Looking at it purely from a language standpoint, rust would probably be my choice if it had support for named parameters. Kotlin is another possibility, but strangely enough I'm not sure if the IDE support is good enough to go with it over Java, even the language was made by JetBrains... Will have to try it out. Kotlin can run on the server, be used for Android development, and can transpile to JavaScript.
Aleksey Kladov
@matklad
Nov 08 2017 17:40
I vouch for Kotlin :)
Restioson
@Restioson
Nov 08 2017 17:40
kotlin kotlin kotlin!!
still-dreaming-1
@still-dreaming-1
Nov 08 2017 17:41
Haha, how is the refactoring and code formatting support in JetBrains IDE compared to Java?
Restioson
@Restioson
Nov 08 2017 17:41
Kotlin is by jetbrains
so pretty great
Aleksey Kladov
@matklad
Nov 08 2017 17:42
Well, its worse then Java at the moment, but I still think it's pretty much excellent
Restioson
@Restioson
Nov 08 2017 17:42
I was wondering the other day about writing a backend for a crossplatform app in rust and then various frontends in kotlin for android, rust for desktop, etc etc
@matklad where is it worse? I've never not been able to use a refactoring feature in kotlin
still-dreaming-1
@still-dreaming-1
Nov 08 2017 17:43
Hm. I wish JetBrains had a different mindset with Kotlin. I think if the language and the IDE are made by the same company, it could have even better support than Java.
Aleksey Kladov
@matklad
Nov 08 2017 17:44
For example, there's no structural search/replace for Kotlin yey
yet
Of course we want to make Kotlin support perfect, it's just that Java has a bit of headstart :)
still-dreaming-1
@still-dreaming-1
Nov 08 2017 17:46
@matklad Do you think it will get there? Is making the IDE support as good as Java their priority or just a nice to reach goal in their mind?
Aleksey Kladov
@matklad
Nov 08 2017 17:48
DM'ing, cause this is offtopic :)
Restioson
@Restioson
Nov 08 2017 17:52
@still-dreaming-1 there's also the JetBrains/kotlin gitter but it's kinda dead tbh, then the slack which is a lot more active
it's very active
still-dreaming-1
@still-dreaming-1
Nov 08 2017 18:18
gitter is so much better than slack, can't stand slack