|
35 | 35 | rust_lib = ffi.dlopen(DLPATH)
|
36 | 36 |
|
37 | 37 |
|
| 38 | +def main(): |
| 39 | + assert rust_lib.is_prime(13) == 1 |
| 40 | + assert rust_lib.is_prime(12) == 0 |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == '__main__': |
| 44 | + main() |
| 45 | + |
| 46 | + |
| 47 | +# -*- coding: utf-8 -*- |
| 48 | +import os.path |
| 49 | +import os |
| 50 | +import sys |
| 51 | +from cffi import FFI |
| 52 | + |
| 53 | +# See https://github.com/SimonSapin/hello-pyrust |
| 54 | + |
| 55 | + |
| 56 | +if sys.platform == 'win32': |
| 57 | + DYNAMIC_LIB_FORMAT = '%s.dll' |
| 58 | +elif sys.platform == 'darwin': |
| 59 | + DYNAMIC_LIB_FORMAT = 'lib%s.dylib' |
| 60 | +elif "linux" in sys.platform: |
| 61 | + DYNAMIC_LIB_FORMAT = 'lib%s.so' |
| 62 | +else: |
| 63 | + raise NotImplementedError('No implementation for "{}".' |
| 64 | + ' Supported platforms are ' |
| 65 | + '"win32", "darwin", and "linux"' |
| 66 | + ''.format(sys.platform)) |
| 67 | +ffi = FFI() |
| 68 | +(file_path, _) = os.path.split(__file__) |
| 69 | +cwd = os.getcwd() |
| 70 | + |
| 71 | +h_path = os.path.join(file_path, 'rust', 'src', 'rust_pypi_example.h',) |
| 72 | +h_rel_path = os.path.join(os.curdir, os.path.relpath(h_path, cwd)) |
| 73 | + |
| 74 | +dlib_path = os.path.join(file_path, 'rust', 'target', 'release', |
| 75 | + DYNAMIC_LIB_FORMAT % 'rust_pypi_example') |
| 76 | +dlib_rel_path = os.path.join(os.curdir, os.path.relpath(dlib_path, cwd)) |
| 77 | + |
| 78 | +with open(h_rel_path) as h: |
| 79 | + ffi.cdef(h.read()) |
| 80 | + |
| 81 | +rust_lib = ffi.dlopen(dlib_rel_path) |
| 82 | + |
| 83 | + |
38 | 84 | def main():
|
39 | 85 | assert rust_lib.is_prime(13) == 1
|
40 | 86 | assert rust_lib.is_prime(12) == 0
|
|
0 commit comments