70ed3a48df
This is an implementation of [RFC 578][rfc] which adds a new `std::env` module to replace most of the functionality in the current `std::os` module. More details can be found in the RFC itself, but as a summary the following methods have all been deprecated: [rfc]: https://github.com/rust-lang/rfcs/pull/578 * `os::args_as_bytes` => `env::args` * `os::args` => `env::args` * `os::consts` => `env::consts` * `os::dll_filename` => no replacement, use `env::consts` directly * `os::page_size` => `env::page_size` * `os::make_absolute` => use `env::current_dir` + `join` instead * `os::getcwd` => `env::current_dir` * `os::change_dir` => `env::set_current_dir` * `os::homedir` => `env::home_dir` * `os::tmpdir` => `env::temp_dir` * `os::join_paths` => `env::join_paths` * `os::split_paths` => `env::split_paths` * `os::self_exe_name` => `env::current_exe` * `os::self_exe_path` => use `env::current_exe` + `pop` * `os::set_exit_status` => `env::set_exit_status` * `os::get_exit_status` => `env::get_exit_status` * `os::env` => `env::vars` * `os::env_as_bytes` => `env::vars` * `os::getenv` => `env::var` or `env::var_string` * `os::getenv_as_bytes` => `env::var` * `os::setenv` => `env::set_var` * `os::unsetenv` => `env::remove_var` Many function signatures have also been tweaked for various purposes, but the main changes were: * `Vec`-returning APIs now all return iterators instead * All APIs are now centered around `OsString` instead of `Vec<u8>` or `String`. There is currently on convenience API, `env::var_string`, which can be used to get the value of an environment variable as a unicode `String`. All old APIs are `#[deprecated]` in-place and will remain for some time to allow for migrations. The semantics of the APIs have been tweaked slightly with regard to dealing with invalid unicode (panic instead of replacement). The new `std::env` module is all contained within the `env` feature, so crates must add the following to access the new APIs: #![feature(env)] [breaking-change]
80 lines
2.8 KiB
Rust
80 lines
2.8 KiB
Rust
// Copyright 2014 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 sys::rwlock as imp;
|
|
|
|
/// An OS-based reader-writer lock.
|
|
///
|
|
/// This structure is entirely unsafe and serves as the lowest layer of a
|
|
/// cross-platform binding of system rwlocks. It is recommended to use the
|
|
/// safer types at the top level of this crate instead of this type.
|
|
pub struct RWLock(imp::RWLock);
|
|
|
|
/// Constant initializer for static RWLocks.
|
|
pub const RWLOCK_INIT: RWLock = RWLock(imp::RWLOCK_INIT);
|
|
|
|
impl RWLock {
|
|
/// Acquire shared access to the underlying lock, blocking the current
|
|
/// thread to do so.
|
|
///
|
|
/// Behavior is undefined if the rwlock has been moved between this and any
|
|
/// previous methodo call.
|
|
#[inline]
|
|
pub unsafe fn read(&self) { self.0.read() }
|
|
|
|
/// Attempt to acquire shared access to this lock, returning whether it
|
|
/// succeeded or not.
|
|
///
|
|
/// This function does not block the current thread.
|
|
///
|
|
/// Behavior is undefined if the rwlock has been moved between this and any
|
|
/// previous methodo call.
|
|
#[inline]
|
|
pub unsafe fn try_read(&self) -> bool { self.0.try_read() }
|
|
|
|
/// Acquire write access to the underlying lock, blocking the current thread
|
|
/// to do so.
|
|
///
|
|
/// Behavior is undefined if the rwlock has been moved between this and any
|
|
/// previous methodo call.
|
|
#[inline]
|
|
pub unsafe fn write(&self) { self.0.write() }
|
|
|
|
/// Attempt to acquire exclusive access to this lock, returning whether it
|
|
/// succeeded or not.
|
|
///
|
|
/// This function does not block the current thread.
|
|
///
|
|
/// Behavior is undefined if the rwlock has been moved between this and any
|
|
/// previous methodo call.
|
|
#[inline]
|
|
pub unsafe fn try_write(&self) -> bool { self.0.try_write() }
|
|
|
|
/// Unlock previously acquired shared access to this lock.
|
|
///
|
|
/// Behavior is undefined if the current thread does not have shared access.
|
|
#[inline]
|
|
pub unsafe fn read_unlock(&self) { self.0.read_unlock() }
|
|
|
|
/// Unlock previously acquired exclusive access to this lock.
|
|
///
|
|
/// Behavior is undefined if the current thread does not currently have
|
|
/// exclusive access.
|
|
#[inline]
|
|
pub unsafe fn write_unlock(&self) { self.0.write_unlock() }
|
|
|
|
/// Destroy OS-related resources with this RWLock.
|
|
///
|
|
/// Behavior is undefined if there are any currently active users of this
|
|
/// lock.
|
|
#[inline]
|
|
pub unsafe fn destroy(&self) { self.0.destroy() }
|
|
}
|