X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Fc_types%2Fmod.rs;h=b6b1bbf0ac85a60025ee97aa5a578059ab973f71;hb=47b7d6e36914774534f4a80315c8d38e0424a2db;hp=3d45802e8063850cc2f71983e47daeefce11a304;hpb=db0ed2ef02f28a8b393eb3640959587bfe1a7e52;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 3d45802..b6b1bbf 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -198,6 +198,28 @@ impl IOError { _ => IOError::Other, } } + pub(crate) fn to_rust(&self) -> std::io::Error { + std::io::Error::new(match self { + IOError::NotFound => std::io::ErrorKind::NotFound, + IOError::PermissionDenied => std::io::ErrorKind::PermissionDenied, + IOError::ConnectionRefused => std::io::ErrorKind::ConnectionRefused, + IOError::ConnectionReset => std::io::ErrorKind::ConnectionReset, + IOError::ConnectionAborted => std::io::ErrorKind::ConnectionAborted, + IOError::NotConnected => std::io::ErrorKind::NotConnected, + IOError::AddrInUse => std::io::ErrorKind::AddrInUse, + IOError::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable, + IOError::BrokenPipe => std::io::ErrorKind::BrokenPipe, + IOError::AlreadyExists => std::io::ErrorKind::AlreadyExists, + IOError::WouldBlock => std::io::ErrorKind::WouldBlock, + IOError::InvalidInput => std::io::ErrorKind::InvalidInput, + IOError::InvalidData => std::io::ErrorKind::InvalidData, + IOError::TimedOut => std::io::ErrorKind::TimedOut, + IOError::WriteZero => std::io::ErrorKind::WriteZero, + IOError::Interrupted => std::io::ErrorKind::Interrupted, + IOError::Other => std::io::ErrorKind::Other, + IOError::UnexpectedEof => std::io::ErrorKind::UnexpectedEof, + }, "") + } } #[repr(C)] @@ -387,13 +409,15 @@ impl Str { if self.len == 0 { return ""; } std::str::from_utf8(unsafe { std::slice::from_raw_parts(self.chars, self.len) }).unwrap() } - pub(crate) fn into_string(self) -> String { + pub(crate) fn into_string(mut self) -> String { let bytes = if self.len == 0 { Vec::new() } else if self.chars_is_owned { - unsafe { + let ret = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(unsafe { self.chars as *mut u8 }, self.len)) - }.into() + }.into(); + self.chars_is_owned = false; + ret } else { let mut ret = Vec::with_capacity(self.len); ret.extend_from_slice(unsafe { std::slice::from_raw_parts(self.chars, self.len) });