Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
6c88e46d
R
Rust
项目概览
int
/
Rust
12 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6c88e46d
编写于
7月 22, 2013
作者:
B
Brian Anderson
提交者:
Daniel Micay
7月 24, 2013
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std:rt: args module is not used by win/mac. #7951
上级
206ae575
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
123 addition
and
69 deletion
+123
-69
src/libstd/rt/args.rs
src/libstd/rt/args.rs
+123
-69
未找到文件。
src/libstd/rt/args.rs
浏览文件 @
6c88e46d
...
...
@@ -14,112 +14,166 @@
//! the processes `argc` and `argv` arguments to be stored
//! in a globally-accessible location for use by the `os` module.
//!
//! Only valid to call on linux. Mac and Windows use syscalls to
//! discover the command line arguments.
//!
//! XXX: Would be nice for this to not exist.
//! XXX: This has a lot of C glue for lack of globals.
use
libc
;
use
option
::{
Option
,
Some
,
None
};
use
str
;
use
uint
;
use
unstable
::
finally
::
Finally
;
use
util
;
use
option
::
Option
;
/// One-time global initialization.
pub
unsafe
fn
init
(
argc
:
int
,
argv
:
**
u8
)
{
let
args
=
load_argc_and_argv
(
argc
,
argv
);
put
(
args
);
imp
::
init
(
argc
,
argv
)
}
/// One-time global cleanup.
pub
fn
cleanup
()
{
rtassert!
(
take
()
.is_some
());
imp
::
cleanup
()
}
/// Take the global arguments from global storage.
pub
fn
take
()
->
Option
<~
[
~
str
]
>
{
with_lock
(||
unsafe
{
let
ptr
=
get_global_ptr
();
let
val
=
util
::
replace
(
&
mut
*
ptr
,
None
);
val
.map
(|
s
:
&~~
[
~
str
]|
(
**
s
)
.clone
())
})
imp
::
take
()
}
/// Give the global arguments to global storage.
///
/// It is an error if the arguments already exist.
pub
fn
put
(
args
:
~
[
~
str
])
{
with_lock
(||
unsafe
{
let
ptr
=
get_global_ptr
();
rtassert!
((
*
ptr
)
.is_none
());
(
*
ptr
)
=
Some
(
~
args
.clone
());
})
imp
::
put
(
args
)
}
/// Make a clone of the global arguments.
pub
fn
clone
()
->
Option
<~
[
~
str
]
>
{
with_lock
(||
unsafe
{
let
ptr
=
get_global_ptr
();
(
*
ptr
)
.map
(|
s
:
&~~
[
~
str
]|
(
**
s
)
.clone
())
})
imp
::
clone
()
}
fn
with_lock
<
T
>
(
f
:
&
fn
()
->
T
)
->
T
{
do
(||
{
unsafe
{
rust_take_global_args_lock
();
f
()
}
})
.finally
{
unsafe
{
rust_drop_global_args_lock
();
}
}
}
#[cfg(target_os
=
"linux"
)]
#[cfg(target_os
=
"android"
)]
#[cfg(target_os
=
"freebsd"
)]
mod
imp
{
fn
get_global_ptr
()
->
*
mut
Option
<~~
[
~
str
]
>
{
unsafe
{
rust_get_global_args_ptr
()
}
}
use
libc
;
use
option
::{
Option
,
Some
,
None
};
use
str
;
use
uint
;
use
unstable
::
finally
::
Finally
;
use
util
;
// Copied from `os`.
unsafe
fn
load_argc_and_argv
(
argc
:
int
,
argv
:
**
u8
)
->
~
[
~
str
]
{
let
mut
args
=
~
[];
for
uint
::
range
(
0
,
argc
as
uint
)
|
i
|
{
args
.push
(
str
::
raw
::
from_c_str
(
*
(
argv
as
**
libc
::
c_char
)
.offset
(
i
)));
pub
unsafe
fn
init
(
argc
:
int
,
argv
:
**
u8
)
{
let
args
=
load_argc_and_argv
(
argc
,
argv
);
put
(
args
);
}
return
args
;
}
extern
{
fn
rust_take_global_args_lock
();
fn
rust_drop_global_args_lock
();
fn
rust_get_global_args_ptr
()
->
*
mut
Option
<~~
[
~
str
]
>
;
}
#[cfg(test)]
mod
tests
{
use
option
::{
Some
,
None
};
use
super
::
*
;
use
unstable
::
finally
::
Finally
;
pub
fn
cleanup
()
{
rtassert!
(
take
()
.is_some
());
}
#[test]
fn
smoke_test
()
{
// Preserve the actual global state.
let
saved_value
=
take
();
pub
fn
take
()
->
Option
<~
[
~
str
]
>
{
with_lock
(||
unsafe
{
let
ptr
=
get_global_ptr
();
let
val
=
util
::
replace
(
&
mut
*
ptr
,
None
);
val
.map
(|
s
:
&~~
[
~
str
]|
(
**
s
)
.clone
())
})
}
let
expected
=
~
[
~
"happy"
,
~
"today?"
];
pub
fn
put
(
args
:
~
[
~
str
])
{
with_lock
(||
unsafe
{
let
ptr
=
get_global_ptr
();
rtassert!
((
*
ptr
)
.is_none
());
(
*
ptr
)
=
Some
(
~
args
.clone
());
})
}
put
(
expected
.clone
());
assert
!
(
clone
()
==
Some
(
expected
.clone
()));
assert
!
(
take
()
==
Some
(
expected
.clone
()));
assert
!
(
take
()
==
None
);
pub
fn
clone
()
->
Option
<~
[
~
str
]
>
{
with_lock
(||
unsafe
{
let
ptr
=
get_global_ptr
();
(
*
ptr
)
.map
(|
s
:
&~~
[
~
str
]|
(
**
s
)
.clone
())
})
}
fn
with_lock
<
T
>
(
f
:
&
fn
()
->
T
)
->
T
{
do
(||
{
unsafe
{
rust_take_global_args_lock
();
f
()
}
})
.finally
{
// Restore the actual global state.
match
saved_value
{
Some
(
ref
args
)
=>
put
(
args
.clone
()),
None
=>
()
unsafe
{
rust_drop_global_args_lock
();
}
}
}
fn
get_global_ptr
()
->
*
mut
Option
<~~
[
~
str
]
>
{
unsafe
{
rust_get_global_args_ptr
()
}
}
// Copied from `os`.
unsafe
fn
load_argc_and_argv
(
argc
:
int
,
argv
:
**
u8
)
->
~
[
~
str
]
{
let
mut
args
=
~
[];
for
uint
::
range
(
0
,
argc
as
uint
)
|
i
|
{
args
.push
(
str
::
raw
::
from_c_str
(
*
(
argv
as
**
libc
::
c_char
)
.offset
(
i
)));
}
return
args
;
}
extern
{
fn
rust_take_global_args_lock
();
fn
rust_drop_global_args_lock
();
fn
rust_get_global_args_ptr
()
->
*
mut
Option
<~~
[
~
str
]
>
;
}
#[cfg(test)]
mod
tests
{
use
option
::{
Some
,
None
};
use
super
::
*
;
use
unstable
::
finally
::
Finally
;
#[test]
fn
smoke_test
()
{
// Preserve the actual global state.
let
saved_value
=
take
();
let
expected
=
~
[
~
"happy"
,
~
"today?"
];
put
(
expected
.clone
());
assert
!
(
clone
()
==
Some
(
expected
.clone
()));
assert
!
(
take
()
==
Some
(
expected
.clone
()));
assert
!
(
take
()
==
None
);
do
(||
{
})
.finally
{
// Restore the actual global state.
match
saved_value
{
Some
(
ref
args
)
=>
put
(
args
.clone
()),
None
=>
()
}
}
}
}
}
#[cfg(target_os
=
"macos"
)]
#[cfg(target_os
=
"win32"
)]
mod
imp
{
pub
unsafe
fn
init
(
_
argc
:
int
,
_
argv
:
**
u8
)
{
}
pub
fn
cleanup
()
{
}
pub
fn
take
()
->
Option
<~
[
~
str
]
>
{
fail
!
()
}
pub
fn
put
(
_
args
:
~
[
~
str
])
{
fail
!
()
}
pub
fn
clone
()
->
Option
<~
[
~
str
]
>
{
fail
!
()
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录