These are chat archives for rust-lang/rust

21st
Oct 2017
Pete Hayes
@petehayes102
Oct 21 2017 09:50

Hey guys, I have a result type that I want a function to be able to return, where T can be any boxed Serialize+Deserialize. Unfortunately Rust doesn’t let me combine traits like that, so I’ve resorted to this:

#[derive(Serialize, Deserialize)]
pub enum EncodableResult<T> {
    Ok(Box<T>),
    Err(String),
}

However this doesn’t work either because the compiler picks up on the fact that T can have different types within the same function. I seem to be between a rock and a hard place. Any ideas?

Jonas Platte
@jplatte
Oct 21 2017 09:57
@petehayes102 You mean E0255? https://doc.rust-lang.org/error-index.html#E0225
Pete Hayes
@petehayes102
Oct 21 2017 09:57
Yep, that’s the one
Denis Lisov
@tanriol
Oct 21 2017 10:00
Well, Send and Sync are exceptions :-)
Jonas Platte
@jplatte
Oct 21 2017 10:00
@tanriol Yeah I saw that when experimenting
But it only applies when they are at the end
Denis Lisov
@tanriol
Oct 21 2017 10:01
It almost works like
trait SerDe<'a>: Serialize + Deserialize<'a> {}

impl<'a, T> SerDe<'a> for T where T: Serialize + Deserialize<'a> {}

fn test() -> Box<SerDe<'static>> {
    ()
}
Jonas Platte
@jplatte
Oct 21 2017 10:01
Box<Send + Sync + Borrow<u32>> doesn't work
I just needed a few traits from the stdlib that had T: ?Sized ^^
Denis Lisov
@tanriol
Oct 21 2017 10:01
Except that these Serialize and Deserialize need to be from erased_serde as the normal serde ones are not object-safe.
Jonas Platte
@jplatte
Oct 21 2017 10:02
I guess I could have used Borrow<u32> + AsRef<u32>
Yeah I wanted to avoid looking at that issue at the same time and just provide an example solution to E0255
But erased_serde?
Pete Hayes
@petehayes102
Oct 21 2017 10:03
Yeah I’m using erased_serde
Jonas Platte
@jplatte
Oct 21 2017 10:03
Aren't SerializeOwned + DeseraliazeOwned part of serde now?
Or is that yet another thing?
search is crap apparently
but I'm not saying this is the same thing
still early in the morning here, I'm not fully booted up ^^
Maybe they solve two completely different problems
Pete Hayes
@petehayes102
Oct 21 2017 10:06
Oh…weird. I shouldn’t have been lazy!
Denis Lisov
@tanriol
Oct 21 2017 10:06
This is another thing. The normal Serialize uses a generic method, which can't be used in a trait object (how would you put it into a vtable?)
Pete Hayes
@petehayes102
Oct 21 2017 10:06
Anyway, I kinda dropped the ball on this one - Deserialize requires Sized, so a trait object is out
Jonas Platte
@jplatte
Oct 21 2017 10:11
@petehayes102 Why did you want a boxed trait object in the first place?
Pete Hayes
@petehayes102
Oct 21 2017 10:11
Becuase I don’t know the return type at compile time
Jonas Platte
@jplatte
Oct 21 2017 10:12
What about an enum then?
Pete Hayes
@petehayes102
Oct 21 2017 10:12
Basically I’m writing a router of sorts, so while each arm has a fixed type, when I put them into a match statement, rust complains that I am returning different types.
Yeah I suppose. I might end up duplicating something like serde’s Value enum
Jonas Platte
@jplatte
Oct 21 2017 10:15
What is this code for that you're writing?
Pete Hayes
@petehayes102
Oct 21 2017 10:17
I’m creating an API that can be pointed at your local machine or a remote machine of the user’s choosing. The API should be consistent, irrespective of the kind of host you’re talking to.
Under the hood I’m proxying requests to an agent on the remote machine (via Tokio), which then runs the endpoint locally and returns a result
I have an enum that maps the function I want to run, plus its args, but was trying to return any type that could be serialized by the Tokio codec
You’re probably right though that another enum is the way to go. I was just hoping for something a bit more concise.
Jonas Platte
@jplatte
Oct 21 2017 10:19
concise?
Pete Hayes
@petehayes102
Oct 21 2017 10:20
Yeah, writing Box::new(blah) is easier than maintaining an enum with all possible return values
But has its own issues, obvs.
Jonas Platte
@jplatte
Oct 21 2017 10:20
If it's just that one function, I don't see a big problem
Pete Hayes
@petehayes102
Oct 21 2017 10:21
It’s lots of functions across several modules
Jonas Platte
@jplatte
Oct 21 2017 10:21
You would just need to add Serialize / Deserialize impls for the enum, so you could use it in the same manner as the boxed thing
Pete Hayes
@petehayes102
Oct 21 2017 10:21
I have a router function that evaluates the enum to figure out which function to call, but there are lots of possible options
Yep. I agree. I was being lazy :)
Pete Hayes
@petehayes102
Oct 21 2017 10:27
@jplatte btw, thank you for your help mate.
Jonas Platte
@jplatte
Oct 21 2017 10:27
no problem!
Nicolas Almy
@almynic
Oct 21 2017 10:31
how can I subtract vectors in rust? I mean like vec1 - vec2 = resultvec. Do I really need to implement a minus and plus and all other operations to use vector for vector calculations?
i think i will just use nalgebra for that
Jeremy Lempereur
@o0Ignition0o
Oct 21 2017 11:15
@almynic Have you tried playing a bit with https://docs.rs/euclid/0.15.4/euclid/struct.TypedVector2D.html ? You get add , add_assign, sub, and sub_assign operators
Zakarum
@omni-viral
Oct 21 2017 11:20
@almynic About what vectors you are talking about? If you mean std::collections::Vec it isn't a vector known from algebra. So it has no operators for vector math. You can look at nalgebra, cgmath and euler for linear algebra and stuff
Denis Lisov
@tanriol
Oct 21 2017 11:21
And ndarray
TatriX
@TatriX
Oct 21 2017 17:24
Do you have problems with racer when working with git or path dependencies?
TatriX
@TatriX
Oct 21 2017 17:52
Ok, I found why: racer-rust/racer#804