rust/hg-cpython/src/lib.rs
author Pierre-Yves David <pierre-yves.david@octobus.net>
Fri, 13 Dec 2019 19:59:59 +0100
changeset 43951 f98f0e3ddaa1
parent 43289 8d432d3a2d7c
child 43966 b69d5f3a41d0
permissions -rw-r--r--
rust-index: add a function to convert PyObject index for hg-core Function in hg-core need something implementing the `Graph` trait. Right now, the `hg-cpython` entry points directly turn the PyObject passed as argument into a `cindex::Index`. However, if we start having the option to use an Index in Rust, we need to dispatch between the different possible PyObject we could receive. So move the "duplicate" call into a unified function. When time come. It will be easy to update the logic of all interface when the time come. Differential Revision: https://phab.mercurial-scm.org/D7653
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     1
// lib.rs
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     2
//
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     3
// Copyright 2018 Georges Racinet <gracinet@anybox.fr>
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     4
//
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     7
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     8
//! Python bindings of `hg-core` objects using the `cpython` crate.
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     9
//! Once compiled, the resulting single shared library object can be placed in
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    10
//! the `mercurial` package directly as `rustext.so` or `rustext.dll`.
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    11
//! It holds several modules, so that from the point of view of Python,
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    12
//! it behaves as the `cext` package.
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    13
//!
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    14
//! Example:
41184
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 41131
diff changeset
    15
//!
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 41131
diff changeset
    16
//! ```text
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    17
//! >>> from mercurial.rustext import ancestor
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    18
//! >>> ancestor.__doc__
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    19
//! 'Generic DAG ancestor algorithms - Rust implementation'
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    20
//! ```
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    21
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
    22
/// This crate uses nested private macros, `extern crate` is still needed in
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
    23
/// 2018 edition.
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    24
#[macro_use]
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    25
extern crate cpython;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    26
41184
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 41131
diff changeset
    27
pub mod ancestors;
41055
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents: 40978
diff changeset
    28
mod cindex;
41240
ff333620a4cc rust-cpython: moved generic conversion fn out of ancestors module
Georges Racinet <georges.racinet@octobus.net>
parents: 41184
diff changeset
    29
mod conversion;
42768
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42763
diff changeset
    30
#[macro_use]
30320c7bf79f rust-cpython: add macro for sharing references
Raphaël Gomès <rgomes@octobus.net>
parents: 42763
diff changeset
    31
pub mod ref_sharing;
41705
0c7b353ce100 rust-cpython: binding for headrevs()
Georges Racinet <georges.racinet@octobus.net>
parents: 41240
diff changeset
    32
pub mod dagops;
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    33
pub mod dirstate;
42217
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents: 41705
diff changeset
    34
pub mod discovery;
41184
dcf818267bc1 rust-cpython: rustdoc improvements
Georges Racinet <georges.racinet@octobus.net>
parents: 41131
diff changeset
    35
pub mod exceptions;
42350
94f3a73b6672 rust-filepatterns: add `rust-cpython` bindings for `filepatterns`
Raphaël Gomès <rgomes@octobus.net>
parents: 42330
diff changeset
    36
pub mod filepatterns;
42829
6a551a2dc666 rust: run cargo fmt
Raphaël Gomès <rgomes@octobus.net>
parents: 42768
diff changeset
    37
pub mod parsers;
43951
f98f0e3ddaa1 rust-index: add a function to convert PyObject index for hg-core
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43289
diff changeset
    38
pub mod revlog;
43251
970978975574 rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents: 42829
diff changeset
    39
pub mod utils;
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    40
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    41
py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| {
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    42
    m.add(
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    43
        py,
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    44
        "__doc__",
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    45
        "Mercurial core concepts - Rust implementation",
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    46
    )?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    47
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    48
    let dotted_name: String = m.get(py, "__name__")?.extract(py)?;
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    49
    m.add(py, "ancestor", ancestors::init_module(py, &dotted_name)?)?;
41705
0c7b353ce100 rust-cpython: binding for headrevs()
Georges Racinet <georges.racinet@octobus.net>
parents: 41240
diff changeset
    50
    m.add(py, "dagop", dagops::init_module(py, &dotted_name)?)?;
42217
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents: 41705
diff changeset
    51
    m.add(py, "discovery", discovery::init_module(py, &dotted_name)?)?;
42330
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents: 42217
diff changeset
    52
    m.add(py, "dirstate", dirstate::init_module(py, &dotted_name)?)?;
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    53
    m.add(
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    54
        py,
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    55
        "filepatterns",
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    56
        filepatterns::init_module(py, &dotted_name)?,
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    57
    )?;
42763
760a7851e9ba rust-parsers: move parser bindings to their own file and Python module
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    58
    m.add(
760a7851e9ba rust-parsers: move parser bindings to their own file and Python module
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    59
        py,
760a7851e9ba rust-parsers: move parser bindings to their own file and Python module
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    60
        "parsers",
760a7851e9ba rust-parsers: move parser bindings to their own file and Python module
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    61
        parsers::init_parsers_module(py, &dotted_name)?,
760a7851e9ba rust-parsers: move parser bindings to their own file and Python module
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    62
    )?;
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    63
    m.add(py, "GraphError", py.get_type::<exceptions::GraphError>())?;
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    64
    m.add(
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    65
        py,
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    66
        "PatternFileError",
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    67
        py.get_type::<exceptions::PatternFileError>(),
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    68
    )?;
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    69
    m.add(
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    70
        py,
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    71
        "PatternError",
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    72
        py.get_type::<exceptions::PatternError>(),
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42379
diff changeset
    73
    )?;
40978
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    74
    Ok(())
5532823e8c18 rust-cpython: start cpython crate bindings
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    75
});
43289
8d432d3a2d7c rust-cpython: prepare for writing tests that require libpython
Yuya Nishihara <yuya@tcha.org>
parents: 43251
diff changeset
    76
8d432d3a2d7c rust-cpython: prepare for writing tests that require libpython
Yuya Nishihara <yuya@tcha.org>
parents: 43251
diff changeset
    77
#[cfg(not(any(feature = "python27-bin", feature = "python3-bin")))]
8d432d3a2d7c rust-cpython: prepare for writing tests that require libpython
Yuya Nishihara <yuya@tcha.org>
parents: 43251
diff changeset
    78
#[test]
8d432d3a2d7c rust-cpython: prepare for writing tests that require libpython
Yuya Nishihara <yuya@tcha.org>
parents: 43251
diff changeset
    79
#[ignore]
8d432d3a2d7c rust-cpython: prepare for writing tests that require libpython
Yuya Nishihara <yuya@tcha.org>
parents: 43251
diff changeset
    80
fn libpython_must_be_linked_to_run_tests() {
8d432d3a2d7c rust-cpython: prepare for writing tests that require libpython
Yuya Nishihara <yuya@tcha.org>
parents: 43251
diff changeset
    81
    // stub function to tell that some tests wouldn't run
8d432d3a2d7c rust-cpython: prepare for writing tests that require libpython
Yuya Nishihara <yuya@tcha.org>
parents: 43251
diff changeset
    82
}