These are chat archives for rust-lang/rust

11th
Sep 2017
lemonxah
@lemonxah
Sep 11 2017 07:28
good day
Moggers
@Moggers
Sep 11 2017 11:09
What kind of approach should I use for architecting datastore code?
I'm only really familiar with using dependency injection
but thats obv not a very good fit for rust
I was thinking of using factories to just provide different datastore implementations at runtime
Moggers
@Moggers
Sep 11 2017 14:15
anybody around?
Denis Lisov
@tanriol
Sep 11 2017 14:16
Here, but I don't exactly understand your problem...
lemonxah
@lemonxah
Sep 11 2017 14:16
what about having a module that uses the same functions in it so that you can lets say
mysql::insert and mongodb::insert
but i think there might be better ways i am not sure
Moggers
@Moggers
Sep 11 2017 14:18
I think I got it
I'm just experimenting with generics and traits and associated types now..
Moggers
@Moggers
Sep 11 2017 14:30
I'm probably doing this all wrong, but I want to have my MongoRepository be a generic struct MongoRepository<Entity>, so that I can define a bunch of functions that get/set entities. But I'm getting does not have a constant size known at compile-time
but I'm not actually trying to store implementations of an Entity inside my repository
so how come it needs to be Sized?
Restioson
@Restioson
Sep 11 2017 15:00
You want to store it on the stack
Moggers
@Moggers
Sep 11 2017 15:00
yeah I read up more on Sized
Restioson
@Restioson
Sep 11 2017 15:00
It's size must be known for it to be on the stack just like that
Moggers
@Moggers
Sep 11 2017 15:01
Im definitely going about this all the wrong way
Restioson
@Restioson
Sep 11 2017 15:01
Try using a pointer (&T) or a box (Box<T>)
Those are always sized, regardless of the data they point to
Moggers
@Moggers
Sep 11 2017 15:01
I could solve the previous Sized problems I had with that!
but not in this case..
Restioson
@Restioson
Sep 11 2017 15:01
Oh?
Moggers
@Moggers
Sep 11 2017 15:01
what I was actually doing was totally wrong
I more or less wanted to be able to pass types as variables and then call static methods on the types
I have a repository which does generic datastore operations, and for the sake of get operations I need to somehow be able to tell it which implementation of Entity it is actually trying to get
so it can ask the Entity stuff like "hey which collection can I find you in?"
Restioson
@Restioson
Sep 11 2017 15:05
so

difference between

let x = &mut y

and

let mut x = &mut y

?

clippy complained about the second
Moggers
@Moggers
Sep 11 2017 15:07
the second looks more like you're asking the reference itself to be mutable
Restioson
@Restioson
Sep 11 2017 15:07
Yeah, but surely to mutate x x needs to be... oh
it seems like i've answered my own question there
x has some methods that take &mut self... so that is what x is, so no problem!
:smile:
Thanks!
Moggers
@Moggers
Sep 11 2017 15:09
I'm thinking maybe I'll have get take an implementation of Entity that way it can look at whatever is passed in to find stuff like the collection it needs to search for
and which of the fields in the document contains the key
so create an implementation of an Entity which only contains its key, then pass that to get and it will fill out the struct with all the other data
or return a new one with the data
sounds kind of..
Moggers
@Moggers
Sep 11 2017 20:32
fn fetch(&self, Box<Entity>) -> Result<Box<Entity>, Box<Error>>
how come thats trying to make Entity into an object?
even though its in a box
yay fixed
I had to use a generic
Denis Lisov
@tanriol
Sep 11 2017 20:59
Having partially correct entities around does not sound fun... why?
Moggers
@Moggers
Sep 11 2017 21:04
partially correct entities?
Denis Lisov
@tanriol
Sep 11 2017 21:06
Hm... likely I misread. Do you mean having separate types implementing Entity for "key only" and "all data"?
Moggers
@Moggers
Sep 11 2017 21:07
Uhm, Entity is a trait, the trait says "you must be able to turn into a bson::Document, you must be able to tell me which collection you should be in, and what the name of your key is"
and it just represents one of my models
Denis Lisov
@tanriol
Sep 11 2017 21:11
Looking at the signature above, you pass a "key-only" Entity to fetch and get back (if successful) a "complete" Entity. Is that still correct?
Moggers
@Moggers
Sep 11 2017 21:11
fn fetch<T: Entity>(&self, key: &str) -> Result<Box<T>, Box<Error>>
This is what I have now
before, I thought I would have to pass a key only entity and then populate it
Denis Lisov
@tanriol
Sep 11 2017 21:12
This one looks much better :-)
Moggers
@Moggers
Sep 11 2017 21:13
yes I know!!
I didnt think I could call a generic function like f<type>();
I thought I would need to pass something in as a variable to let the function know which type it's operating on
Yehuda Katz
@wycats
Sep 11 2017 21:47
zomg it's Gitter!
fn main() {
  println!("hello world");
}
Aleksey Kladov
@matklad
Sep 11 2017 21:49
Hi @wycats !
Yehuda Katz
@wycats
Sep 11 2017 21:52
:D
Moggers
@Moggers
Sep 11 2017 22:37
IT WORKS
IT LIVES
My generic, dependency injection based repository system works!!!
well its not really dependency injection because I specify the types at compile time..
but still!
Ashley Mannix
@KodrAus
Sep 11 2017 23:58
@Moggers very cool! Is there a link?