Skip to content

Commit

Permalink
don't use Default impls in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
matsadler committed Feb 18, 2023
1 parent e896b1d commit e724386
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 53 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn is_blank(rb_self: String) -> bool {
!rb_self.contains(|c: char| !c.is_whitespace())
}

let class = magnus::define_class("String", Default::default())?;
let class = magnus::define_class("String", magnus::class::object())?;
// 0 as self doesn't count against the number of arguments
class.define_method("blank?", magnus::method!(is_blank, 0))?;
```
Expand Down Expand Up @@ -82,7 +82,7 @@ wrapped in the specified class, and whenever it is passed back to Rust it will
be unwrapped to a reference.

```rust
use magnus::{define_class, function, method, prelude::*, Error};
use magnus::{class, define_class, function, method, prelude::*, Error};

#[magnus::wrap(class = "Point")]
struct Point {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Point {

#[magnus::init]
fn init() -> Result<(), Error> {
let class = define_class("Point", Default::default())?;
let class = define_class("Point", class::object())?;
class.define_singleton_method("new", function!(Point::new, 2))?;
class.define_method("x", method!(Point::x, 0))?;
class.define_method("y", method!(Point::y, 0))?;
Expand Down
4 changes: 2 additions & 2 deletions examples/mut_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cell::RefCell;

use magnus::{define_class, embed, eval, function, method, prelude::*, wrap, Error};
use magnus::{class, define_class, embed, eval, function, method, prelude::*, wrap, Error};

struct Point {
x: isize,
Expand Down Expand Up @@ -39,7 +39,7 @@ impl MutPoint {
fn main() -> Result<(), Error> {
let _cleanup = unsafe { embed::init() };

let class = define_class("Point", Default::default())?;
let class = define_class("Point", class::object())?;
class.define_singleton_method("new", function!(MutPoint::new, 2))?;
class.define_method("x", method!(MutPoint::x, 0))?;
class.define_method("x=", method!(MutPoint::set_x, 1))?;
Expand Down
4 changes: 2 additions & 2 deletions examples/point.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use magnus::{define_class, embed, eval, function, method, prelude::*, wrap, Error};
use magnus::{class, define_class, embed, eval, function, method, prelude::*, wrap, Error};

#[wrap(class = "Point")]
struct Point {
Expand Down Expand Up @@ -27,7 +27,7 @@ impl Point {
fn main() -> Result<(), Error> {
let _cleanup = unsafe { embed::init() };

let class = define_class("Point", Default::default())?;
let class = define_class("Point", class::object())?;
class.define_singleton_method("new", function!(Point::new, 2))?;
class.define_method("x", method!(Point::x, 0))?;
class.define_method("y", method!(Point::y, 0))?;
Expand Down
3 changes: 2 additions & 1 deletion examples/rust_blank/ext/rust_blank/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use magnus::{
class,
define_class,
encoding::{CType, RbEncoding},
method,
Expand Down Expand Up @@ -38,7 +39,7 @@ fn is_blank(rb_self: RString) -> Result<bool, Error> {

#[magnus::init]
fn init() -> Result<(), Error> {
let class = define_class("String", Default::default())?;
let class = define_class("String", class::object())?;
class.define_method("blank?", method!(is_blank, 0))?;
Ok(())
}
4 changes: 2 additions & 2 deletions src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl RClass {
/// use magnus::{class, RClass};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// let class = RClass::new(Default::default()).unwrap();
/// let class = RClass::new(class::object()).unwrap();
/// assert!(class.is_kind_of(class::class()));
/// ```
pub fn new(superclass: RClass) -> Result<RClass, Error> {
Expand Down Expand Up @@ -304,7 +304,7 @@ pub trait Class: Module {
/// ```
/// use magnus::{class, eval, Class};
/// # let _cleanup = unsafe { magnus::embed::init() };
/// let class = magnus::define_class("Point", Default::default()).unwrap();
/// let class = magnus::define_class("Point", class::object()).unwrap();
///
/// class.undef_alloc_func();
///
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! # Examples
//!
//! ```
//! use magnus::{define_module, function, method, prelude::*, Error};
//! use magnus::{class, define_module, function, method, prelude::*, Error};
//!
//! #[magnus::wrap(class = "Euclid::Point", free_immediately, size)]
//! struct Point {
Expand Down Expand Up @@ -86,7 +86,7 @@
//! #[magnus::init]
//! fn init() -> Result<(), Error> {
//! let module = define_module("Euclid")?;
//! let class = module.define_class("Point", Default::default())?;
//! let class = module.define_class("Point", class::object())?;
//! class.define_singleton_method("new", function!(Point::new, 2))?;
//! class.define_method("x", method!(Point::x, 0))?;
//! class.define_method("y", method!(Point::y, 0))?;
Expand Down Expand Up @@ -1924,10 +1924,10 @@ use crate::{
/// # Examples
///
/// ```
/// use magnus::{define_class, memoize, RClass};
/// use magnus::{class, define_class, memoize, RClass};
///
/// fn foo_class() -> &'static RClass {
/// memoize!(RClass: define_class("Foo", Default::default()).unwrap())
/// memoize!(RClass: define_class("Foo", class::object()).unwrap())
/// }
/// ```
#[macro_export]
Expand Down
4 changes: 2 additions & 2 deletions src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2569,15 +2569,15 @@ where
/// # Examples
///
/// ```
/// use magnus::{define_class, method, prelude::*, Error};
/// use magnus::{class, define_class, method, prelude::*, Error};
///
/// fn rb_is_blank(rb_self: String) -> bool {
/// rb_self.contains(|c: char| !c.is_whitespace())
/// }
///
/// #[magnus::init]
/// fn init() -> Result<(), Error> {
/// let class = define_class("String", Default::default())?;
/// let class = define_class("String", class::object())?;
/// class.define_method("blank?", method!(rb_is_blank, 0))?;
/// Ok(())
/// }
Expand Down
20 changes: 10 additions & 10 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub trait Module: Object + Deref<Target = Value> + ReprValue + Copy {
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// let outer = define_module("Outer").unwrap();
/// let inner = outer.define_class("Inner", Default::default()).unwrap();
/// let inner = outer.define_class("Inner", class::object()).unwrap();
/// assert!(inner.is_kind_of(class::class()));
/// ```
fn define_class<T>(self, name: T, superclass: RClass) -> Result<RClass, Error>
Expand Down Expand Up @@ -264,7 +264,7 @@ pub trait Module: Object + Deref<Target = Value> + ReprValue + Copy {
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// let outer = define_module("Outer").unwrap();
/// let inner = outer.define_error("InnerError", Default::default()).unwrap();
/// let inner = outer.define_error("InnerError", exception::standard_error()).unwrap();
/// assert!(inner.is_inherited(exception::standard_error()));
/// ```
fn define_error<T>(self, name: T, superclass: ExceptionClass) -> Result<ExceptionClass, Error>
Expand All @@ -283,7 +283,7 @@ pub trait Module: Object + Deref<Target = Value> + ReprValue + Copy {
/// # Examples
///
/// ```
/// use magnus::{function, Module, RClass, RModule};
/// use magnus::{class, function, Module, RClass, RModule};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// fn example() -> i64 {
Expand All @@ -293,7 +293,7 @@ pub trait Module: Object + Deref<Target = Value> + ReprValue + Copy {
/// let module = RModule::new();
/// module.define_method("example", function!(example, 0)).unwrap();
///
/// let class = RClass::new(Default::default()).unwrap();
/// let class = RClass::new(class::object()).unwrap();
/// class.include_module(module);
///
/// let obj = class.new_instance(()).unwrap();
Expand Down Expand Up @@ -412,10 +412,10 @@ pub trait Module: Object + Deref<Target = Value> + ReprValue + Copy {
/// # Examples
///
/// ```
/// use magnus::{eval, Module, RClass};
/// use magnus::{class, eval, Module, RClass};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// let a = RClass::new(Default::default()).unwrap();
/// let a = RClass::new(class::object()).unwrap();
/// let b = RClass::new(a).unwrap();
/// assert!(b.is_inherited(a));
/// assert!(!a.is_inherited(b));
Expand Down Expand Up @@ -600,10 +600,10 @@ pub trait Module: Object + Deref<Target = Value> + ReprValue + Copy {
/// # Examples
///
/// ```
/// use magnus::{Attr, Module, RClass, Value};
/// use magnus::{class, Attr, Module, RClass, Value};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// let class = RClass::new(Default::default()).unwrap();
/// let class = RClass::new(class::object()).unwrap();
/// class.define_attr("example", Attr::ReadWrite).unwrap();
///
/// let obj = class.new_instance(()).unwrap();
Expand Down Expand Up @@ -633,14 +633,14 @@ pub trait Module: Object + Deref<Target = Value> + ReprValue + Copy {
/// # Examples
///
/// ```
/// use magnus::{function, Module, RClass};
/// use magnus::{class, function, Module, RClass};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// fn example() -> i64 {
/// 42
/// }
///
/// let class = RClass::new(Default::default()).unwrap();
/// let class = RClass::new(class::object()).unwrap();
/// class.define_method("example", function!(example, 0)).unwrap();
/// class.define_alias("test", "example").unwrap();
///
Expand Down
8 changes: 4 additions & 4 deletions src/r_typed_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl RTypedData {
/// # Examples
///
/// ```
/// use magnus::{define_class, RTypedData};
/// use magnus::{class, define_class, RTypedData};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// #[magnus::wrap(class = "Point")]
Expand All @@ -73,7 +73,7 @@ impl RTypedData {
/// y: isize,
/// }
///
/// let point_class = define_class("Point", Default::default()).unwrap();
/// let point_class = define_class("Point", class::object()).unwrap();
///
/// let value = RTypedData::wrap(Point { x: 4, y: 2 });
/// assert!(value.is_kind_of(point_class));
Expand All @@ -91,7 +91,7 @@ impl RTypedData {
/// # Examples
///
/// ```
/// use magnus::{define_class, RTypedData};
/// use magnus::{class, define_class, RTypedData};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// #[magnus::wrap(class = "Point")]
Expand All @@ -101,7 +101,7 @@ impl RTypedData {
/// y: isize,
/// }
///
/// let point_class = define_class("Point", Default::default()).unwrap();
/// let point_class = define_class("Point", class::object()).unwrap();
/// let value = RTypedData::wrap(Point { x: 4, y: 2 });
///
/// assert_eq!(value.get::<Point>().unwrap(), &Point { x: 4, y: 2 });
Expand Down
12 changes: 6 additions & 6 deletions src/scan_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ impl<T> ScanArgsBlock for T where T: private::ScanArgsBlock {}
/// `TCPServer::new`'s argument handling. This is roughly equivalent to
/// `def new(hostname=nil, port)`.
/// ```
/// use magnus::{prelude::*, define_class, error::Error, function, scan_args::scan_args, Value};
/// use magnus::{prelude::*, class, define_class, error::Error, function, scan_args::scan_args, Value};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// fn tcp_svr_init(args: &[Value]) -> Result<Value, Error> {
Expand All @@ -1118,7 +1118,7 @@ impl<T> ScanArgsBlock for T where T: private::ScanArgsBlock {}
/// # Ok(res.into())
/// }
///
/// let class = define_class("TCPServer", Default::default()).unwrap();
/// let class = define_class("TCPServer", class::object()).unwrap();
/// class.define_singleton_method("new", function!(tcp_svr_init, -1)).unwrap();
/// # let res = magnus::eval::<bool>(r#"TCPServer.new("foo", 1) == ["foo", 1]"#).unwrap();
/// # assert!(res);
Expand All @@ -1128,7 +1128,7 @@ impl<T> ScanArgsBlock for T where T: private::ScanArgsBlock {}
///
/// The same example as above, specifying the types slightly differently.
/// ```
/// use magnus::{prelude::*, define_class, error::Error, function, scan_args::scan_args, Value};
/// use magnus::{prelude::*, class, define_class, error::Error, function, scan_args::scan_args, Value};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// fn tcp_svr_init(args: &[Value]) -> Result<Value, Error> {
Expand All @@ -1143,7 +1143,7 @@ impl<T> ScanArgsBlock for T where T: private::ScanArgsBlock {}
/// # Ok(res.into())
/// }
///
/// let class = define_class("TCPServer", Default::default()).unwrap();
/// let class = define_class("TCPServer", class::object()).unwrap();
/// class.define_singleton_method("new", function!(tcp_svr_init, -1)).unwrap();
/// # let res = magnus::eval::<bool>(r#"TCPServer.new("foo", 1) == ["foo", 1]"#).unwrap();
/// # assert!(res);
Expand All @@ -1155,7 +1155,7 @@ impl<T> ScanArgsBlock for T where T: private::ScanArgsBlock {}
/// `def getaddrinfo(nodename, service, family=nil, socktype=nil, protocol=nil, flags=nil, timeout: nil)`.
/// ```
/// use magnus::{
/// prelude::*, define_class, error::Error, function, scan_args::{scan_args, get_kwargs}, RHash, Symbol, Value,
/// prelude::*, class, define_class, error::Error, function, scan_args::{scan_args, get_kwargs}, RHash, Symbol, Value,
/// };
/// # let _cleanup = unsafe { magnus::embed::init() };
///
Expand Down Expand Up @@ -1183,7 +1183,7 @@ impl<T> ScanArgsBlock for T where T: private::ScanArgsBlock {}
/// # Ok(res.into())
/// }
///
/// let class = define_class("Addrinfo", Default::default()).unwrap();
/// let class = define_class("Addrinfo", class::object()).unwrap();
/// class.define_singleton_method("getaddrinfo", function!(getaddrinfo, -1)).unwrap();
/// # let res = magnus::eval::<bool>(r#"Addrinfo.getaddrinfo("a", 1) == ["a", 1, nil, nil, nil, nil, nil]"#).unwrap();
/// # assert!(res);
Expand Down
20 changes: 10 additions & 10 deletions src/typed_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ where
/// # Examples
///
/// ```
/// use magnus::{prelude::*, define_class, memoize, RClass, TypedData};
/// use magnus::{prelude::*, class, define_class, memoize, RClass, TypedData};
/// # use magnus::DataType;
///
/// struct Example();
///
/// unsafe impl TypedData for Example {
/// fn class() -> RClass {
/// *memoize!(RClass: {
/// let class = define_class("Example", Default::default()).unwrap();
/// let class = define_class("Example", class::object()).unwrap();
/// class.undef_alloc_func();
/// class
/// })
Expand Down Expand Up @@ -500,7 +500,7 @@ where
/// # Examples
///
/// ```
/// use magnus::{define_class, typed_data};
/// use magnus::{class, define_class, typed_data};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// #[magnus::wrap(class = "Point")]
Expand All @@ -509,7 +509,7 @@ where
/// y: isize,
/// }
///
/// let point_class = define_class("Point", Default::default()).unwrap();
/// let point_class = define_class("Point", class::object()).unwrap();
///
/// let value = typed_data::Obj::wrap(Point { x: 4, y: 2 });
/// assert!(value.is_kind_of(point_class));
Expand All @@ -524,7 +524,7 @@ where
/// # Examples
///
/// ```
/// use magnus::{define_class, typed_data};
/// use magnus::{class, define_class, typed_data};
/// # let _cleanup = unsafe { magnus::embed::init() };
///
/// #[magnus::wrap(class = "Point")]
Expand All @@ -534,7 +534,7 @@ where
/// y: isize,
/// }
///
/// let point_class = define_class("Point", Default::default()).unwrap();
/// let point_class = define_class("Point", class::object()).unwrap();
/// let value = typed_data::Obj::wrap(Point { x: 4, y: 2 });
///
/// assert_eq!(value.get(), &Point { x: 4, y: 2 });
Expand Down Expand Up @@ -658,7 +658,7 @@ where
/// use std::hash::Hasher;
///
/// use magnus::{
/// prelude::*, define_class, embed::init, function, gc, method, typed_data, DataTypeFunctions, Error,
/// prelude::*, class, define_class, embed::init, function, gc, method, typed_data, DataTypeFunctions, Error,
/// RHash, TypedData, Value,
/// };
///
Expand Down Expand Up @@ -711,7 +711,7 @@ where
///
/// let _cleanup = unsafe { init() };
///
/// let class = define_class("Pair", Default::default()).unwrap();
/// let class = define_class("Pair", class::object()).unwrap();
/// class
/// .define_singleton_method("new", function!(Pair::new, 2))
/// .unwrap();
Expand Down Expand Up @@ -764,7 +764,7 @@ where
/// use std::hash::Hasher;
///
/// use magnus::{
/// prelude::*, define_class, embed::init, function, gc, method, typed_data, DataTypeFunctions, Error,
/// prelude::*, class, define_class, embed::init, function, gc, method, typed_data, DataTypeFunctions, Error,
/// RHash, TypedData, Value,
/// };
///
Expand Down Expand Up @@ -817,7 +817,7 @@ where
///
/// let _cleanup = unsafe { init() };
///
/// let class = define_class("Pair", Default::default()).unwrap();
/// let class = define_class("Pair", class::object()).unwrap();
/// class
/// .define_singleton_method("new", function!(Pair::new, 2))
/// .unwrap();
Expand Down
Loading

0 comments on commit e724386

Please sign in to comment.