These are chat archives for rust-lang/rust

10th
Oct 2018
Conner Bryan
@cab
Oct 10 2018 14:51
if I have some API that I want to be implemented by multiple modules (essentially different backends), which can be switched by a feature, is there some idiomatic way to ensure that all the implementations stay in sync?
Michal 'vorner' Vaner
@vorner
Oct 10 2018 14:54
@cab I'm not sure what you mean by API, but it sounds pretty much like a trait to me.
Conner Bryan
@cab
Oct 10 2018 15:01
each module has a fn do_foo, fn do_bar, etc, that should all take the same parameters -- is it possible to say that a module implements a trait? or should I have a struct ModuleB; impl API for ModuleB, and then just do use ModuleB::*?
Michal 'vorner' Vaner
@vorner
Oct 10 2018 15:02
No, you need a type to implement a trait, but that type can be empty. eg. struct BackendA; impl Api for BackendA { ... }
Conner Bryan
@cab
Oct 10 2018 15:02
oops sorry, that's what I meant with my latter example, poor wording on my part. thanks! I'll do that
ah, and the other missing link was associated types, just found those docs
Conner Bryan
@cab
Oct 10 2018 15:22
is there any way/equivalent to use associated functions?
Michal 'vorner' Vaner
@vorner
Oct 10 2018 16:59
use? You can have functions that don't take self (in any form), and call them by the BackendA::function(). Or are you asking something else? You can just forward/call the other function from inside.
cubetastic
@cubetastic33
Oct 10 2018 17:05
What is the difference between serde-yaml and yaml-rust? Why should I prefer any one of these over the other?
Michal 'vorner' Vaner
@vorner
Oct 10 2018 17:08
@cubetastic33 The serde-yaml integrates the yaml-rust parser into the serde system. So you can either use the low-level one, or use it together with all the derives and such.
cubetastic
@cubetastic33
Oct 10 2018 17:08
@vorner What does this serde system do? What advantages does it have?
Sylwester Rąpała
@xoac
Oct 10 2018 17:09
Single API for all serialize deserialize types. It allow use derive with serde_derive crate.
cubetastic
@cubetastic33
Oct 10 2018 17:10
@xoac so, it is only particularly advantageous if I'm using multiple file formats?
Sylwester Rąpała
@xoac
Oct 10 2018 17:11
for me it's that you don't care about format. You just care about serialization format :)
cubetastic
@cubetastic33
Oct 10 2018 17:11
what do you mean?
Sylwester Rąpała
@xoac
Oct 10 2018 17:11
and then u call serde_json(&my_struck) or serde_yml(&my_struct)
cubetastic
@cubetastic33
Oct 10 2018 17:12
what does "serialization format" mean?
Sylwester Rąpała
@xoac
Oct 10 2018 17:13
it's mean that your struct implemet Serialize trait https://serde.rs/impl-serialize.html
cubetastic
@cubetastic33
Oct 10 2018 17:13
so - I could just use rust-yaml and serialize & deserialize my data, right?
Would there be a considerable difficulty difference?
Sylwester Rąpała
@xoac
Oct 10 2018 17:16

I never used serialization / deserialization crates directly. In most cases u want write:

#[derive(Deserialize, Serialize)]
struct {
  a: u32
}

and don't care u serialize using json or ymal. You don't event care how (de)serialization functions looks like.

but if u need not standard (de)serialization you can write own function. Read serde.rs site. It's with json examples but it will work with yaml as well
Jonathan Steyfkens
@jonathansty
Oct 10 2018 21:45
Anyone an idea how I get a Vec from a C styled array as in pointers, I've got a *mut and a size. The *mut is coming from some C library
Denis Lisov
@tanriol
Oct 10 2018 21:47
You shall not do that in general. Most likely you want to make a slice, not a Vec.
Jonathan Steyfkens
@jonathansty
Oct 10 2018 21:54
@tanriol right, I just found that out :)