These are chat archives for rust-lang/rust

18th
Dec 2017
Sergey Noskov
@Albibek
Dec 18 2017 10:54
@Shaddy seems like deadlock, due to write lock being held until main ends
Sherab Giovannini
@Shaddy
Dec 18 2017 10:54
oh
thought it was a problem with how I defined Send and Sync
thanks :)
David
@yenicelik
Dec 18 2017 12:47
Hey guys
So I'm trying to implement a function that can take multiple different types that all are 'similar' (e.g. all are tables from diesel-databases, or same struct types), and works with these
This is the function I'm trying to implement:

pub fn get_most_recent_entry<T: Ticker>(conn: &SqliteConnection) {
    let res = T::table
        .order(T::time.desc())
        .limit(1)
        .load::<T::record>(&*conn);
    match res {
        Ok(x) => {
            if x.len() > 0 {
                Ok(x.get(0).unwrap().time)
            } else {
                Ok(0)
            }
        }
        Err(err) => Err(format_err!("Something went wrong retrieving the most recently inserted \
        bitcoin record! {:?}", err))
    }
}
And I initially tried out implementing a trait Ticker which looks and implements the following way:
trait Ticker {
    type table: diesel::associations::HasTable;
    type time;
    type record;
}

struct BTC;

impl Ticker for BTC {
    type table = types::bitcoin::table;
    type time = types::bitcoin::time;
    type record = types::BTCRecord;
}

pub fn establish_connection() -> Result<SqliteConnection, Error> {
    match SqliteConnection::establish("/Users/unamesrc/database/sqlite_database.db") {
        Ok(x) => Ok(x),
        Err(err) => Err(format_err!("data was not found within the struct! {:?}", err))
    }
}
However, when trying to compile these, I get the following error:
error[E0599]: no function or associated item named `table` found for type `T` in the current scope
  --> src/database/populate_db.rs:82:15
   |
82 |     let res = T::table
   |               ^^^^^^^^ function or associated item not found in `T`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `table`, perhaps you need to implement it:
           candidate #1: `diesel::associations::HasTable`
What am I doing wrong? I am completely new to associated types, so these are quite confusing to me :sweat:
Restioson
@Restioson
Dec 18 2017 13:14
1) just a style thing, types should still be camel case
Hm, that's very strange. Could you perhaps give us a playground demonstrating the issue?
David
@yenicelik
Dec 18 2017 13:16
I'd have to re-implement the diesel database using that :D just gimme some time
Restioson
@Restioson
Dec 18 2017 13:16
No no, just extract the bits for associated types
David
@yenicelik
Dec 18 2017 13:17
yh sure then :)
Restioson
@Restioson
Dec 18 2017 13:17
another general style thing, acronyms like BTC (i'm assuming it's an acronym -- if it's Bitcoin then it still applies) should be like this: Btc
for for instance that trait for ticker and then a function that simply prints the value of T::table
David
@yenicelik
Dec 18 2017 13:18
thx for the style things, i'll change it later then :)
for for instance that trait for ticker and then a function that simply prints the value of T::table
what do you mean by this specifically? Is it ok if I just put everything in the playground and you tell me what is missing?
Restioson
@Restioson
Dec 18 2017 13:19
WHat do you mean by 'everything'?
David
@yenicelik
Dec 18 2017 13:20
ok, so this will probably not run bcs diesel is missing :D but maybe it's more helpful than the things above?
https://play.rust-lang.org/?gist=ecba3276aea2058351fc27e2d85ea5f7&version=stable
WHat do you mean by 'everything'? -> the code lines relevan
t
Restioson
@Restioson
Dec 18 2017 13:20
yes, that is what I meant :smile:
David
@yenicelik
Dec 18 2017 13:20
:D
Restioson
@Restioson
Dec 18 2017 13:29
it seems to work
David
@yenicelik
Dec 18 2017 13:30
hmm
a difference I see is the function signature to fn test_associated_type<T: MyTrait>(_: T) { (the argument)
bcs I don't have the argument implemented as type T
Do I have to do that?
Restioson
@Restioson
Dec 18 2017 13:31
ok, i'll make it more like yours
David
@yenicelik
Dec 18 2017 13:31
I mean it's fine :D i don't care how it works as long as it works :sweat: :D
Restioson
@Restioson
Dec 18 2017 13:31
I'm not sure why yours isn't working
David
@yenicelik
Dec 18 2017 13:31
me neither hahaha
w8
Restioson
@Restioson
Dec 18 2017 13:31
I feel like it should
David
@yenicelik
Dec 18 2017 13:32
Thx! :)
just as additional information
Restioson
@Restioson
Dec 18 2017 13:32
Sorry I couldn't help more
David
@yenicelik
Dec 18 2017 13:32

by initial problem is shown here:
https://play.rust-lang.org/?gist=0b00867cca3c60bdf14a7faa7312101f&version=undefined

i have two functions that both do exactly the same thing, but the input and output types are a little different

is there a common solution to that kind of problem, because it feels like simple generics should cover it?
Restioson
@Restioson
Dec 18 2017 13:34
It should
David
@yenicelik
Dec 18 2017 13:35
So instead of get_most_recent_eth_entryI should be able to do
get_most_recent_entry<TableType, RecordType >() -> ...?
Restioson
@Restioson
Dec 18 2017 13:35
yes, assuming you do it right
David
@yenicelik
Dec 18 2017 13:36
ok, i'll tinker around a little more, thanks a lot anyways :)
David
@yenicelik
Dec 18 2017 14:09
Ok, now this doesn't seem to make sense at all
pub trait Ticker {
    type Table;
}

struct Btc;

impl Ticker for Btc {
    type Table = i32;
}

pub fn get_most_recent_entry<T>(conn: &SqliteConnection) -> () where T: Ticker {
    T::Table;
}
and it tells me

error[E0599]: no associated item named `Table` found for type `T` in the current scope
  --> src/database/populate_db.rs:58:5
   |
58 |     T::Table;
   |     ^^^^^^^^ associated item not found in `T`

error: aborting due to previous error
wtf is going on :D I must be blind or just plain stupid at this point
Restioson
@Restioson
Dec 18 2017 14:10
btw you dont need the -> ()
very strange
are you sure there isn't another Ticker trait?
David
@yenicelik
Dec 18 2017 14:12
Changing Ticker to TickerBTCBTCBTC produces the same error.. (i assume no one uses the name TickerBTCBTCBTC anywhere :D )
Restioson
@Restioson
Dec 18 2017 14:12
that is very strange
David
@yenicelik
Dec 18 2017 14:12
I would've prefered to be stupid; then the issue would've been resolved
Restioson
@Restioson
Dec 18 2017 14:12
haha
David
@yenicelik
Dec 18 2017 14:13
should I open a github issue? or i'll first update rust i guess
Restioson
@Restioson
Dec 18 2017 14:13
It works for other cases, so I'd say no
let me try something
David
@yenicelik
Dec 18 2017 14:14
I'll just update rust real quick... let's hope that resolves it D:
Denis Lisov
@tanriol
Dec 18 2017 14:14
@yenicelik You latest code is expected not to work :-)
David
@yenicelik
Dec 18 2017 14:14
why not :D
please enlighten me :D
Restioson
@Restioson
Dec 18 2017 14:14
Me too haha
oberien
@oberien
Dec 18 2017 14:14
let foo: T::Table; works just fine.
But T::Table itself is not an expression I guess.
Restioson
@Restioson
Dec 18 2017 14:15
I don't think it matters what they do with it
Well, I mean
oberien
@oberien
Dec 18 2017 14:15
If you exchange your line 58 with let foo: T::Table; then it'll compile.
Restioson
@Restioson
Dec 18 2017 14:15
In previous examples they've used it properly to no avail
oberien
@oberien
Dec 18 2017 14:16
(Just joined, so I didn't look at those)
Restioson
@Restioson
Dec 18 2017 14:16
wait wait a second
Bernhard Schuster
@drahnr
Dec 18 2017 14:16
How do I make sure two streams are executed on the same tokio core loop?
I have a stream of tasks TaskStream and a custom Protocol XProto which sits ontop of a TcpConnection implemented using tokio proto.
What would be an idiomatic way to solve this?
Or is the best way to use a FuturesUnordered?
Restioson
@Restioson
Dec 18 2017 14:16
even this shouldn't work? @yenicelik
    let res = T::table
        .order(T::time.desc())
        .limit(1)
        .load::<T::record>(&*conn);
You call a method on an associated type??
oberien
@oberien
Dec 18 2017 14:17
@drahnr I think it'd be better to ask that in the tokio gitter :)
David
@yenicelik
Dec 18 2017 14:17
so I tried out different things... I am not sure what diesel returns, but it seems to be a type weirdly..
Restioson
@Restioson
Dec 18 2017 14:17
I think your code unrelated to the assoc type is wrong
David
@yenicelik
Dec 18 2017 14:18
so basically the mistake is to call a function on the associated type?
so the diesel return type must be an expression somehow?
Restioson
@Restioson
Dec 18 2017 14:18
no no
Show me types::bitcoin::table??
Question: does your inital code work?? Lmao
oberien
@oberien
Dec 18 2017 14:19

I think I may have found an auto-trait (OIBIT) bug. If I have an auto trait and !implement it for a tuple with generic bounds (impl<A, B> !FooAuto for (A, B) where A: Foo, B: Foo {}), that rule seems to apply to every 2-tuple no matter of trait bounds: https://play.rust-lang.org/?gist=88ca6b9da0581befd09cf295936cb3ae&version=nightly

trait Foo {}
auto trait FooAuto {}

impl<A, B> !FooAuto for (A, B) where A: Foo, B: Foo {}
fn test<A>(a: A) where A: FooAuto {}

fn main() {
    test((1.0f64, 1i32));
}

fails with

the trait `FooAuto` is not implemented for `(f64, i32)`

Do I miss something obvious?

David
@yenicelik
Dec 18 2017 14:20
Question: does your inital code work?? Lmao ->
:D yes, it does :D
Restioson
@Restioson
Dec 18 2017 14:20
Ok...
David
@yenicelik
Dec 18 2017 14:20
i updated rust and have some other probs now, lemme just resolve those... then i can print types::bitcoin::table
Restioson
@Restioson
Dec 18 2017 14:20
I think something strange is happening
What is types::bitcoin::table
show me the decl?
David
@yenicelik
Dec 18 2017 14:22
it's something the diesel library produces through macros... i would need to get the updated rust working before i can 'error' the type... it's one of these, which is not really helpful as of now i guess https://docs.rs/diesel/1.0.0-beta1/diesel/?search=table
lemme just resolve the update thing
Restioson
@Restioson
Dec 18 2017 14:22
I'm not sure how you can call a method taking self on types::bitcoin::table
oberien
@oberien
Dec 18 2017 14:28
Should I open a rust-issue for my problem?
Restioson
@Restioson
Dec 18 2017 14:56
Perhaps. If you don't want to go that far, you could open a thread.
So it seems I've discovered a gnarly bug somewhere upstream of Manish's array-init
I say somewhere because I don't know whether it's nodrop or rlibc
or something upstream of nodrop
but now QEMU/gdb is being very annoying
Kelly Thomas Kline
@kellytk
Dec 18 2017 20:24
If you're interested in an in-depth article on Tokio https://cafbit.com/post/tokio_internals/ was published today and IMHO it's very well written
ashley williams
@ashleygwilliams
Dec 18 2017 20:25
oh neat!
thanks for sharing
Kelly Thomas Kline
@kellytk
Dec 18 2017 20:25
+1