datafusion_ffi/
volatility.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use abi_stable::StableAbi;
19use datafusion::logical_expr::Volatility;
20
21#[repr(C)]
22#[derive(Debug, StableAbi, Clone)]
23#[allow(non_camel_case_types)]
24pub enum FFI_Volatility {
25    Immutable,
26    Stable,
27    Volatile,
28}
29
30impl From<Volatility> for FFI_Volatility {
31    fn from(value: Volatility) -> Self {
32        match value {
33            Volatility::Immutable => Self::Immutable,
34            Volatility::Stable => Self::Stable,
35            Volatility::Volatile => Self::Volatile,
36        }
37    }
38}
39
40impl From<&FFI_Volatility> for Volatility {
41    fn from(value: &FFI_Volatility) -> Self {
42        match value {
43            FFI_Volatility::Immutable => Self::Immutable,
44            FFI_Volatility::Stable => Self::Stable,
45            FFI_Volatility::Volatile => Self::Volatile,
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use datafusion::logical_expr::Volatility;
53
54    use super::FFI_Volatility;
55
56    fn test_round_trip_volatility(volatility: Volatility) {
57        let ffi_volatility: FFI_Volatility = volatility.into();
58        let round_trip: Volatility = (&ffi_volatility).into();
59
60        assert_eq!(volatility, round_trip);
61    }
62
63    #[test]
64    fn test_all_round_trip_volatility() {
65        test_round_trip_volatility(Volatility::Immutable);
66        test_round_trip_volatility(Volatility::Stable);
67        test_round_trip_volatility(Volatility::Volatile);
68    }
69}