Last active
August 29, 2015 14:25
-
-
Save YoEight/1842de681651195336a3 to your computer and use it in GitHub Desktop.
WTF rustc ??? (Builder pattern)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's because
Connection
andCredentials
don't deriveCopy
. I can't do#[derive(Copy,Clone)]
forCredentials
but now I know why it doesn't work.