summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorMathias Svensson <freaken@freaken.dk>2019-10-28 05:20:29 +0100
committerRobert Winslow <rw@users.noreply.github.com>2019-10-28 00:20:29 -0400
commitb4774d2354425b29b233170cdef23c97c03d4aa8 (patch)
treebd5fc4af932954309eceda4a9daca7a55924e619 /rust
parent26f238c24888bf02a872f304fe9f03433fd0dd00 (diff)
downloadflatbuffers-b4774d2354425b29b233170cdef23c97c03d4aa8.tar.gz
flatbuffers-b4774d2354425b29b233170cdef23c97c03d4aa8.tar.bz2
flatbuffers-b4774d2354425b29b233170cdef23c97c03d4aa8.zip
Rust: Fix Copy and Clone impls for a few generic types (#5577)
* Rust: Fix Copy and Clone impls for a few generic types * Add tests for Copy+Clone * Wrap Copy+Clone checks in a #[test] function
Diffstat (limited to 'rust')
-rw-r--r--rust/flatbuffers/src/primitives.rs35
-rw-r--r--rust/flatbuffers/src/vector.rs12
2 files changed, 41 insertions, 6 deletions
diff --git a/rust/flatbuffers/src/primitives.rs b/rust/flatbuffers/src/primitives.rs
index ccab9386..06a22eae 100644
--- a/rust/flatbuffers/src/primitives.rs
+++ b/rust/flatbuffers/src/primitives.rs
@@ -79,9 +79,21 @@ pub struct VTableWIPOffset {}
/// data relative to the *end* of an in-progress FlatBuffer. The
/// FlatBufferBuilder uses this to track the location of objects in an absolute
/// way. The impl of Push converts a WIPOffset into a ForwardsUOffset.
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug)]
pub struct WIPOffset<T>(UOffsetT, PhantomData<T>);
+// We cannot use derive for these two impls, as the derived impls would only
+// implement `Copy` and `Clone` for `T: Copy` and `T: Clone` respectively.
+// However `WIPOffset<T>` can always be copied, no matter that `T` you
+// have.
+impl<T> Copy for WIPOffset<T> {}
+impl<T> Clone for WIPOffset<T> {
+ #[inline(always)]
+ fn clone(&self) -> Self {
+ *self
+ }
+}
+
impl<T> PartialEq for WIPOffset<T> {
fn eq(&self, o: &WIPOffset<T>) -> bool {
self.value() == o.value()
@@ -108,12 +120,12 @@ impl<'a, T: 'a> WIPOffset<T> {
/// Return a wrapped value that brings its meaning as a union WIPOffset
/// into the type system.
#[inline(always)]
- pub fn as_union_value(&self) -> WIPOffset<UnionWIPOffset> {
+ pub fn as_union_value(self) -> WIPOffset<UnionWIPOffset> {
WIPOffset::new(self.0)
}
/// Get the underlying value.
#[inline(always)]
- pub fn value(&self) -> UOffsetT {
+ pub fn value(self) -> UOffsetT {
self.0
}
}
@@ -139,11 +151,24 @@ impl<T> Push for ForwardsUOffset<T> {
/// ForwardsUOffset is used by Follow to traverse a FlatBuffer: the pointer
/// is incremented by the value contained in this type.
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug)]
pub struct ForwardsUOffset<T>(UOffsetT, PhantomData<T>);
+
+// We cannot use derive for these two impls, as the derived impls would only
+// implement `Copy` and `Clone` for `T: Copy` and `T: Clone` respectively.
+// However `ForwardsUOffset<T>` can always be copied, no matter that `T` you
+// have.
+impl<T> Copy for ForwardsUOffset<T> {}
+impl<T> Clone for ForwardsUOffset<T> {
+ #[inline(always)]
+ fn clone(&self) -> Self {
+ *self
+ }
+}
+
impl<T> ForwardsUOffset<T> {
#[inline(always)]
- pub fn value(&self) -> UOffsetT {
+ pub fn value(self) -> UOffsetT {
self.0
}
}
diff --git a/rust/flatbuffers/src/vector.rs b/rust/flatbuffers/src/vector.rs
index de97abb4..ef1986ab 100644
--- a/rust/flatbuffers/src/vector.rs
+++ b/rust/flatbuffers/src/vector.rs
@@ -25,9 +25,19 @@ use endian_scalar::{read_scalar, read_scalar_at};
use follow::Follow;
use primitives::*;
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug)]
pub struct Vector<'a, T: 'a>(&'a [u8], usize, PhantomData<T>);
+// We cannot use derive for these two impls, as it would only implement Copy
+// and Clone for `T: Copy` and `T: Clone` respectively. However `Vector<'a, T>`
+// can always be copied, no matter that `T` you have.
+impl<'a, T> Copy for Vector<'a, T> {}
+impl<'a, T> Clone for Vector<'a, T> {
+ fn clone(&self) -> Self {
+ *self
+ }
+}
+
impl<'a, T: 'a> Vector<'a, T> {
#[inline(always)]
pub fn new(buf: &'a [u8], loc: usize) -> Self {