mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-05 06:27:38 +00:00

This commit is an implementation of [RFC 592][r592] and [RFC 840][r840]. These two RFCs tweak the behavior of `CString` and add a new `CStr` unsized slice type to the module. [r592]: https://github.com/rust-lang/rfcs/blob/master/text/0592-c-str-deref.md [r840]: https://github.com/rust-lang/rfcs/blob/master/text/0840-no-panic-in-c-string.md The new `CStr` type is only constructable via two methods: 1. By `deref`'ing from a `CString` 2. Unsafely via `CStr::from_ptr` The purpose of `CStr` is to be an unsized type which is a thin pointer to a `libc::c_char` (currently it is a fat pointer slice due to implementation limitations). Strings from C can be safely represented with a `CStr` and an appropriate lifetime as well. Consumers of `&CString` should now consume `&CStr` instead to allow producers to pass in C-originating strings instead of just Rust-allocated strings. A new constructor was added to `CString`, `new`, which takes `T: IntoBytes` instead of separate `from_slice` and `from_vec` methods (both have been deprecated in favor of `new`). The `new` method returns a `Result` instead of panicking. The error variant contains the relevant information about where the error happened and bytes (if present). Conversions are provided to the `io::Error` and `old_io::IoError` types via the `FromError` trait which translate to `InvalidInput`. This is a breaking change due to the modification of existing `#[unstable]` APIs and new deprecation, and more detailed information can be found in the two RFCs. Notable breakage includes: * All construction of `CString` now needs to use `new` and handle the outgoing `Result`. * Usage of `CString` as a byte slice now explicitly needs a `.as_bytes()` call. * The `as_slice*` methods have been removed in favor of just having the `as_bytes*` methods. Closes #22469 Closes #22470 [breaking-change]
75 lines
2.0 KiB
Rust
75 lines
2.0 KiB
Rust
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
|
|
use prelude::v1::*;
|
|
|
|
use ffi::CStr;
|
|
use io;
|
|
use libc::{self, c_int, size_t};
|
|
use str;
|
|
use sys::c;
|
|
use net::{SocketAddr, IpAddr};
|
|
use sys::fd::FileDesc;
|
|
use sys_common::AsInner;
|
|
|
|
pub use sys::{cvt, cvt_r};
|
|
|
|
pub type wrlen_t = size_t;
|
|
|
|
pub struct Socket(FileDesc);
|
|
|
|
pub fn init() {}
|
|
|
|
pub fn cvt_gai(err: c_int) -> io::Result<()> {
|
|
if err == 0 { return Ok(()) }
|
|
|
|
let detail = unsafe {
|
|
str::from_utf8(CStr::from_ptr(c::gai_strerror(err)).to_bytes()).unwrap()
|
|
.to_string()
|
|
};
|
|
Err(io::Error::new(io::ErrorKind::Other,
|
|
"failed to lookup address information", Some(detail)))
|
|
}
|
|
|
|
impl Socket {
|
|
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
|
|
let fam = match addr.ip() {
|
|
IpAddr::V4(..) => libc::AF_INET,
|
|
IpAddr::V6(..) => libc::AF_INET6,
|
|
};
|
|
unsafe {
|
|
let fd = try!(cvt(libc::socket(fam, ty, 0)));
|
|
Ok(Socket(FileDesc::new(fd)))
|
|
}
|
|
}
|
|
|
|
pub fn accept(&self, storage: *mut libc::sockaddr,
|
|
len: *mut libc::socklen_t) -> io::Result<Socket> {
|
|
let fd = try!(cvt_r(|| unsafe {
|
|
libc::accept(self.0.raw(), storage, len)
|
|
}));
|
|
Ok(Socket(FileDesc::new(fd)))
|
|
}
|
|
|
|
pub fn duplicate(&self) -> io::Result<Socket> {
|
|
cvt(unsafe { libc::dup(self.0.raw()) }).map(|fd| {
|
|
Socket(FileDesc::new(fd))
|
|
})
|
|
}
|
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
self.0.read(buf)
|
|
}
|
|
}
|
|
|
|
impl AsInner<c_int> for Socket {
|
|
fn as_inner(&self) -> &c_int { self.0.as_inner() }
|
|
}
|