1 // Copyright (c) Facebook, Inc. and its affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 use std::convert::TryInto;
8 use bitflags::bitflags;
10 // NB: Keep the values of these flags in sync with shallow_decl_defs.ml.
13 pub struct PropFlags: u8 {
14 const ABSTRACT = 1 << 0;
16 const LATEINIT = 1 << 2;
18 const NEEDS_INIT = 1 << 4;
19 const PHP_STD_LIB = 1 << 5;
20 const READONLY = 1 << 6;
24 impl ocamlrep::ToOcamlRep for PropFlags {
25 fn to_ocamlrep<'a, A: ocamlrep::Allocator>(&self, _alloc: &'a A) -> ocamlrep::OpaqueValue<'a> {
26 ocamlrep::OpaqueValue::int(self.bits() as isize)
30 impl ocamlrep::FromOcamlRep for PropFlags {
31 fn from_ocamlrep(value: ocamlrep::Value<'_>) -> Result<Self, ocamlrep::FromError> {
32 let int_value = ocamlrep::from::expect_int(value)?;
33 Ok(Self::from_bits_truncate(int_value.try_into()?))
37 impl<'a> ocamlrep::FromOcamlRepIn<'a> for PropFlags {
39 value: ocamlrep::Value<'_>,
40 _alloc: &'a bumpalo::Bump,
41 ) -> Result<Self, ocamlrep::FromError> {
42 use ocamlrep::FromOcamlRep;
43 Self::from_ocamlrep(value)
47 impl no_pos_hash::NoPosHash for PropFlags {
48 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
49 std::hash::Hash::hash(self, state);
53 impl serde::Serialize for PropFlags {
54 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
55 serializer.serialize_u8(self.bits())
59 impl<'de> serde::Deserialize<'de> for PropFlags {
60 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
62 impl<'de> serde::de::Visitor<'de> for Visitor {
63 type Value = PropFlags;
65 fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
66 write!(formatter, "a u8 for PropFlags")
68 fn visit_u8<E: serde::de::Error>(self, value: u8) -> Result<Self::Value, E> {
69 Ok(Self::Value::from_bits_truncate(value))
72 deserializer.deserialize_u8(Visitor)