Skip to content

Instantly share code, notes, and snippets.

@coolreader18
Last active January 4, 2019 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolreader18/224cfa96ad8b7e2cb62dd12bb31efdfa to your computer and use it in GitHub Desktop.
Save coolreader18/224cfa96ad8b7e2cb62dd12bb31efdfa to your computer and use it in GitHub Desktop.
Different versions of io.rs's mk_module, using macros or not
// the expanded version, with attributes and variable names cleaned up
let ctx: &PyContext = ctx;
let py_mod = ctx.new_module("io", ctx.new_scope(None));
let IOBase = {
ctx.new_class("IOBase", ctx.object())
};
ctx.set_attr(&py_mod, "IOBase", IOBase.clone());
let StringIO = {
let py_class = ctx.new_class("StringIO", IOBase.clone());
let __init__ = ctx.new_rustfunc(string_io_init);
ctx.set_attr(&py_class, "__init__", __init__.clone());
let getvalue = ctx.new_rustfunc(string_io_getvalue);
ctx.set_attr(&py_class, "getvalue", getvalue.clone());
py_class
};
ctx.set_attr(&py_mod, "StringIO", StringIO.clone());
let BytesIO = {
let py_class = ctx.new_class("BytesIO", IOBase.clone());
let __init__ = ctx.new_rustfunc(bytes_io_init);
ctx.set_attr(&py_class, "__init__", __init__.clone());
let getvalue = ctx.new_rustfunc(bytes_io_getvalue);
ctx.set_attr(&py_class, "getvalue", getvalue.clone());
py_class
};
ctx.set_attr(&py_mod, "BytesIO", BytesIO.clone());
py_mod
let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
let io_base = ctx.new_class("IOBase", ctx.object());
ctx.set_attr(&py_mod, "IOBase", io_base.clone());
let string_io = ctx.new_class("StringIO", io_base.clone());
ctx.set_attr(&string_io, "__init__", ctx.new_rustfunc(string_io_init));
ctx.set_attr(&string_io, "getvalue", ctx.new_rustfunc(string_io_getvalue));
ctx.set_attr(&py_mod, "StringIO", string_io);
let bytes_io = ctx.new_class("BytesIO", io_base.clone());
ctx.set_attr(&bytes_io, "__init__", ctx.new_rustfunc(bytes_io_init));
ctx.set_attr(&bytes_io, "getvalue", ctx.new_rustfunc(bytes_io_getvalue));
ctx.set_attr(&py_mod, "BytesIO", bytes_io);
py_mod
{
let __ctx: &PyContext = ctx;
let __var = {
let __ctx: &PyContext = __ctx;
{
let __py_mod = __ctx.new_module("io", __ctx.new_scope(None));
#[allow(non_snake_case)]
let IOBase = {
let __py_class = __ctx.new_class("IOBase", __ctx.object());
__py_class
};
__ctx.set_attr(&__py_mod, "IOBase", IOBase.clone());
#[allow(non_snake_case)]
let StringIO = {
let __py_class = __ctx.new_class("StringIO", IOBase.clone());
#[allow(non_snake_case)]
let __init__ = __ctx.new_rustfunc(string_io_init);
__ctx.set_attr(&__py_class, "__init__", __init__.clone());
#[allow(non_snake_case)]
let getvalue = __ctx.new_rustfunc(string_io_getvalue);
__ctx.set_attr(&__py_class, "getvalue", getvalue.clone());
__py_class
};
__ctx.set_attr(&__py_mod, "StringIO", StringIO.clone());
#[allow(non_snake_case)]
let BytesIO = {
let __py_class = __ctx.new_class("BytesIO", IOBase.clone());
#[allow(non_snake_case)]
let __init__ = __ctx.new_rustfunc(bytes_io_init);
__ctx.set_attr(&__py_class, "__init__", __init__.clone());
#[allow(non_snake_case)]
let getvalue = __ctx.new_rustfunc(bytes_io_getvalue);
__ctx.set_attr(&__py_class, "getvalue", getvalue.clone());
__py_class
};
__ctx.set_attr(&__py_mod, "BytesIO", BytesIO.clone());
__py_mod
}
};
__var
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment