Skip to content

Instantly share code, notes, and snippets.

@YoEight
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoEight/1842de681651195336a3 to your computer and use it in GitHub Desktop.
Save YoEight/1842de681651195336a3 to your computer and use it in GitHub Desktop.
WTF rustc ??? (Builder pattern)
$ cargo build
Compiling project v0.1.0 (file:///home/yoeight/code/rust/project-driver)
src/project/internal/types.rs:48:30: 48:34 error: cannot move out of borrowed content
src/project/internal/types.rs:48 connection_type: self.connection_type,
^~~~
src/project/internal/types.rs:50:20: 50:24 error: cannot move out of borrowed content
src/project/internal/types.rs:50 creds: self.creds
^~~~
error: aborting due to 2 previous errors
Could not compile `project`.
To learn more, run the command again with --verbose.
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::net::SocketAddrV4;
use std::net::ToSocketAddrs;
pub enum ConnectionType {
Regular,
}
pub enum Credentials {
NoCreds,
Creds(String, String),
}
pub struct Settings {
pub connection_type: ConnectionType,
pub store_addr: SocketAddr,
pub creds: Credentials
}
pub struct SettingsBuilder {
connection_type: ConnectionType,
store_addr: SocketAddr,
creds: Credentials
}
impl SettingsBuilder {
pub fn new(ip: Ipv4Addr, port: u16) -> SettingsBuilder {
SettingsBuilder {
connection_type: ConnectionType::Regular,
store_addr: SocketAddr::V4(SocketAddrV4::new(ip, port)),
creds: Credentials::NoCreds
}
}
pub fn set_connection_type(&mut self, tpe: ConnectionType) -> &mut SettingsBuilder {
self.connection_type = tpe;
self
}
pub fn set_credentials(&mut self, creds: Credentials) -> &mut SettingsBuilder {
self.creds = creds;
self
}
pub fn finalize(&self) -> Settings { // if I drop the '&' it compiles
Settings {
connection_type: self.connection_type,
store_addr: self.store_addr,
creds: self.creds
}
}
}
@YoEight
Copy link
Author

YoEight commented Jul 22, 2015

It's because Connection and Credentials don't derive Copy. I can't do #[derive(Copy,Clone)] for Credentials but now I know why it doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment