Make examples adhere to API guidelines
> Examples use ?, not try!, not unwrap (C-QUESTION-MARK) > Like it or not, example code is often copied verbatim by users. > Unwrapping an error should be a conscious decision that the user > needs to make.
This commit is contained in:
parent
69fa01df45
commit
9b809a8197
36
src/lua.rs
36
src/lua.rs
|
@ -176,16 +176,20 @@ impl<'lua> String<'lua> {
|
|||
///
|
||||
/// ```
|
||||
/// # extern crate rlua;
|
||||
/// # use rlua::{Lua, String};
|
||||
/// # fn main() {
|
||||
/// # use rlua::{Lua, String, Result};
|
||||
/// # fn try_main() -> Result<()> {
|
||||
/// let lua = Lua::new();
|
||||
/// let globals = lua.globals();
|
||||
///
|
||||
/// let version: String = globals.get("_VERSION").unwrap();
|
||||
/// let version: String = globals.get("_VERSION")?;
|
||||
/// assert!(version.to_str().unwrap().contains("Lua"));
|
||||
///
|
||||
/// let non_utf8: String = lua.eval(r#" "test\xff" "#, None).unwrap();
|
||||
/// let non_utf8: String = lua.eval(r#" "test\xff" "#, None)?;
|
||||
/// assert!(non_utf8.to_str().is_err());
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// # fn main() {
|
||||
/// # try_main().unwrap();
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn to_str(&self) -> Result<&str> {
|
||||
|
@ -509,19 +513,22 @@ impl<'lua> Function<'lua> {
|
|||
///
|
||||
/// ```
|
||||
/// # extern crate rlua;
|
||||
/// # use rlua::{Lua, Function};
|
||||
///
|
||||
/// # fn main() {
|
||||
/// # use rlua::{Lua, Function, Result};
|
||||
/// # fn try_main() -> Result<()> {
|
||||
/// let lua = Lua::new();
|
||||
/// let globals = lua.globals();
|
||||
///
|
||||
/// // Bind the argument `123` to Lua's `tostring` function
|
||||
/// let tostring: Function = globals.get("tostring").unwrap();
|
||||
/// let tostring_123: Function = tostring.bind(123i32).unwrap();
|
||||
/// let tostring: Function = globals.get("tostring")?;
|
||||
/// let tostring_123: Function = tostring.bind(123i32)?;
|
||||
///
|
||||
/// // Now we can call `tostring_123` without arguments to get the result of `tostring(123)`
|
||||
/// let result: String = tostring_123.call(()).unwrap();
|
||||
/// let result: String = tostring_123.call(())?;
|
||||
/// assert_eq!(result, "123");
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// # fn main() {
|
||||
/// # try_main().unwrap();
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn bind<A: ToLuaMulti<'lua>>(&self, args: A) -> Result<Function<'lua>> {
|
||||
|
@ -603,9 +610,8 @@ impl<'lua> Thread<'lua> {
|
|||
///
|
||||
/// ```
|
||||
/// # extern crate rlua;
|
||||
/// # use rlua::*;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// # use rlua::{Lua, Thread, Error, Result};
|
||||
/// # fn try_main() -> Result<()> {
|
||||
/// let lua = Lua::new();
|
||||
/// let thread: Thread = lua.eval(r#"
|
||||
/// coroutine.create(function(arg)
|
||||
|
@ -624,6 +630,10 @@ impl<'lua> Thread<'lua> {
|
|||
/// Err(Error::CoroutineInactive) => {},
|
||||
/// unexpected => panic!("unexpected result {:?}", unexpected),
|
||||
/// }
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// # fn main() {
|
||||
/// # try_main().unwrap();
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn resume<A, R>(&self, args: A) -> Result<R>
|
||||
|
|
Loading…
Reference in New Issue