Add ByteRange/StringPiece Conversion for Cython
authorJason Fried <fried@fb.com>
Mon, 13 Nov 2017 21:32:49 +0000 (13:32 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 13 Nov 2017 21:35:45 +0000 (13:35 -0800)
Summary:
Moving the cython definition to folly/python.
Adding simple conversion helper to_bytes

This is to cut down on duplicate folly::range cython definitions

Reviewed By: yfeldblum

Differential Revision: D6291125

fbshipit-source-id: 314b732a1516a03fb5c9a57939552bbabd81970b

folly/python/range.pxd [new file with mode: 0644]

diff --git a/folly/python/range.pxd b/folly/python/range.pxd
new file mode 100644 (file)
index 0000000..fef1dd9
--- /dev/null
@@ -0,0 +1,17 @@
+cdef extern from "folly/Range.h" namespace "folly":
+    cdef cppclass Range[T]:
+        Range()
+        Range(T, int)
+        T data()
+        int size()
+
+ctypedef Range[const char*] StringPiece
+ctypedef Range[const unsigned char*] ByteRange
+
+ctypedef fused R:
+    StringPiece
+    ByteRange
+
+# Conversion Helpers
+cdef inline bytes to_bytes(R range):
+    return <bytes>range.data()[:range.size()]