Skip to content

Commit

Permalink
Move AnyLifetime from gazebo to starlark
Browse files Browse the repository at this point in the history
Summary: [Explanations](https://fb.workplace.com/groups/buck2dev/posts/3377928935828488).

Reviewed By: ndmitchell, pallotron

Differential Revision: D42539399

fbshipit-source-id: 10a2444d5359c750f9098e07d7bce68cfc41363f
  • Loading branch information
stepancheg authored and facebook-github-bot committed Jan 18, 2023
1 parent 9e23ba4 commit c43b8a6
Show file tree
Hide file tree
Showing 49 changed files with 98 additions and 80 deletions.
1 change: 0 additions & 1 deletion gazebo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ This library contains a collection of well-tested utilities. Most modules stand
* `gazebo::prelude::*` is intended to be imported as such, and provides extension traits to common types. For example, it provides `Vec::map` which is equivalent to `iter().map(f).collect::<Vec<_>>()`, and `str::split1` like `split` but which only splits once. We hope some of these functions one day make it into the Rust standard library.
* `gazebo::dupe` provides the trait `Dupe` with the member `dupe`, all of which are exactly like `Clone`. The difference is that `Dupe` should not be implemented for types that reallocate or have expensive `clone` operations - e.g. there is `Dupe` for `Arc` and `usize`, but not for `String` and `Vec`. By using `dupe` it is easy to focus on the `clone` calls (which should be rare) and ignore things whose cost is minimal.
* `gazebo::cell::ARef` provides a type which is either a `Ref<T>` or a direct reference `&T`, with operations that make it look like `Ref` -- allowing you to uniformly convert a reference into something like a `Ref`.
* `gazebo::any::AnyLifetime` provides a trait like `Any`, but which does not require `'static` lifetimes, at the cost of more boilerplate.

The functionality provided by Gazebo is not stable, and continues to evolve with both additions (as we find new useful features) and removals (as we find better patterns or libraries encapsulating the ideas better). While the code varies in usefulness and design quality, it is all well tested and documented.

Expand Down
1 change: 0 additions & 1 deletion gazebo/gazebo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

//! A collection of well-tested primitives that have been useful. Most modules stand alone.
pub mod any;
pub mod cast;
pub mod cell;
pub mod cmp;
Expand Down
8 changes: 0 additions & 8 deletions gazebo/gazebo_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#[allow(unused_extern_crates)] // proc_macro is very special
extern crate proc_macro;

mod any_lifetime;
mod coerce;
mod default;
mod variant;
Expand All @@ -24,13 +23,6 @@ pub fn derive_default_(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
default::derive_default_(input)
}

/// Derive the `ProvidesStaticType` trait. Requires the type has no type arguments, no constant arguments,
/// and at most one lifetime argument.
#[proc_macro_derive(ProvidesStaticType)]
pub fn derive_provides_static_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
any_lifetime::derive_provides_static_type(input)
}

/// Derive the `VariantName` trait.
#[proc_macro_derive(VariantName)]
pub fn derive_variant_names(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand Down
27 changes: 17 additions & 10 deletions gazebo/gazebo/src/any.rs → starlark/src/any.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* Copyright 2019 The Starlark in Rust Authors.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//! Methods that build upon the [`Any` trait](std::any::Any).
Expand All @@ -18,7 +26,7 @@ use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;

pub use gazebo_derive::ProvidesStaticType;
pub use starlark_derive::ProvidesStaticType;

/// Provides access to the same type as `Self` but with all lifetimes dropped to `'static`
/// (including lifetimes of parameters).
Expand Down Expand Up @@ -61,7 +69,7 @@ unsafe impl<'a, T: ProvidesStaticType + 'a + ?Sized> AnyLifetime<'a> for T {
/// `AnyLifetime`:
///
/// ```
/// use gazebo::any::ProvidesStaticType;
/// use starlark::any::ProvidesStaticType;
/// #[derive(ProvidesStaticType)]
/// struct Foo1();
/// #[derive(ProvidesStaticType)]
Expand All @@ -72,7 +80,7 @@ unsafe impl<'a, T: ProvidesStaticType + 'a + ?Sized> AnyLifetime<'a> for T {
/// directly.
///
/// ```
/// use gazebo::any::ProvidesStaticType;
/// use starlark::any::ProvidesStaticType;
/// # fn main() {
/// # use std::fmt::Display;
/// struct Baz<T: Display>(T);
Expand Down Expand Up @@ -243,8 +251,7 @@ mod tests {
use std::fmt::Display;

use super::*;
#[allow(unused_imports)] // Not actually unused, this makes testing the derive macro work
use crate as gazebo;
use crate as starlark;

#[test]
fn test_can_convert() {
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,11 @@ mod tests {
use std::fmt::Formatter;

use allocative::Allocative;
use gazebo::any::ProvidesStaticType;
use starlark_derive::starlark_module;

use super::*;
use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::environment::GlobalsBuilder;
use crate::environment::GlobalsStatic;
use crate::environment::Methods;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/environment/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,10 @@ fn common_documentation<'a>(
#[cfg(test)]
mod tests {
use derive_more::Display;
use gazebo::any::ProvidesStaticType;

use super::*;
use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::assert::Assert;
use crate::starlark_type;
use crate::values::NoSerialize;
Expand Down
3 changes: 2 additions & 1 deletion starlark/src/environment/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ use std::time::Instant;
use allocative::Allocative;
use derive_more::Display;
use dupe::Dupe;
use gazebo::any::ProvidesStaticType;
use itertools::Itertools;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::collections::Hashed;
use crate::docs;
use crate::docs::DocItem;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/eval/compiler/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use allocative::Allocative;
use derivative::Derivative;
use derive_more::Display;
use dupe::Dupe;
use gazebo::any::ProvidesStaticType;
use gazebo::prelude::*;
use once_cell::sync::Lazy;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::codemap::CodeMap;
use crate::collections::Hashed;
use crate::const_frozen_string;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/eval/runtime/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use std::mem::MaybeUninit;
use std::path::Path;

use dupe::Dupe;
use gazebo::any::AnyLifetime;
use gazebo::cast;
use thiserror::Error;

use crate::any::AnyLifetime;
use crate::codemap::FileSpan;
use crate::codemap::FileSpanRef;
use crate::codemap::ResolvedFileSpan;
Expand Down
3 changes: 2 additions & 1 deletion starlark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
//! use starlark::eval::Evaluator;
//! use starlark::syntax::{AstModule, Dialect};
//! use starlark::values::{none::NoneType, Value, ValueLike};
//! use gazebo::any::ProvidesStaticType;
//! use starlark::any::ProvidesStaticType;
//! use std::cell::RefCell;
//!
//! let content = r#"
Expand Down Expand Up @@ -390,6 +390,7 @@ pub use starlark_derive::starlark_module;
pub use starlark_derive::StarlarkDocs;

pub(crate) mod analysis;
pub mod any;
pub mod assert;
pub mod codemap;
pub mod collections;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/stdlib/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ use std::fmt;
use std::fmt::Display;

use allocative::Allocative;
use gazebo::any::ProvidesStaticType;
use gazebo::coerce::coerce;
use gazebo::coerce::Coerce;
use gazebo::prelude::*;
use itertools::Itertools;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::collections::symbol_map::Symbol;
use crate::environment::GlobalsBuilder;
use crate::eval::runtime::arguments::ArgNames;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ mod tests {
use allocative::Allocative;
use derive_more::Display;
use dupe::Dupe;
use gazebo::any::ProvidesStaticType;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::assert::Assert;
use crate::environment::GlobalsBuilder;
use crate::environment::Methods;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/tests/derive/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

use allocative::Allocative;
use derive_more::Display;
use gazebo::any::ProvidesStaticType;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::assert::Assert;
use crate::values::StarlarkAttrs;
use crate::values::StarlarkValue;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/tests/derive/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

use allocative::Allocative;
use derive_more::Display;
use gazebo::any::ProvidesStaticType;
use gazebo::coerce::Coerce;
use serde::Serialize;
use serde::Serializer;
use starlark_derive::StarlarkDocs;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::docs::get_registered_starlark_docs;
use crate::docs::DocItem;
use crate::docs::DocString;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/tests/derive/module/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/

use allocative::Allocative;
use gazebo::any::ProvidesStaticType;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::assert::Assert;
use crate::environment::Methods;
use crate::environment::MethodsBuilder;
Expand Down
5 changes: 2 additions & 3 deletions starlark/src/tests/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use std::sync::Mutex;

use allocative::Allocative;
use derive_more::Display;
use gazebo::any::ProvidesStaticType;
use gazebo::cell::AsARef;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::assert;
use crate::assert::Assert;
use crate::collections::SmallMap;
Expand All @@ -49,9 +49,8 @@ fn test_export_as() {
use std::fmt::Debug;
use std::fmt::Display;

use gazebo::any::ProvidesStaticType;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::values::AllocValue;
use crate::values::Heap;
use crate::values::StarlarkValue;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/tests/opt/if_rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use std::fmt::Display;

use derive_more::Display;
use dupe::Dupe;
use gazebo::any::ProvidesStaticType;
use rand::rngs::SmallRng;
use rand::Rng;
use rand::SeedableRng;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::environment::GlobalsBuilder;
use crate::environment::Module;
use crate::eval::Evaluator;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/tests/uncategorized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use std::fmt::Write;

use allocative::Allocative;
use derive_more::Display;
use gazebo::any::ProvidesStaticType;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::assert;
use crate::assert::Assert;
use crate::collections::SmallMap;
Expand Down
5 changes: 2 additions & 3 deletions starlark/src/values/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
use std::any::TypeId;
use std::marker::PhantomData;

use gazebo::any::AnyLifetime;

use crate::any::AnyLifetime;
use crate::values::Value;

/// Taken by [`StarlarkValue::provide`](crate::values::StarlarkValue::provide)
Expand Down Expand Up @@ -61,9 +60,9 @@ pub(crate) fn request_value_impl<'v, T: AnyLifetime<'v>>(value: Value<'v>) -> Op
#[cfg(test)]
mod tests {
use allocative::Allocative;
use gazebo::any::ProvidesStaticType;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::values::demand::Demand;
use crate::values::Heap;
use crate::values::StarlarkValue;
Expand Down
3 changes: 2 additions & 1 deletion starlark/src/values/layout/avalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ use std::mem;

use allocative::Allocative;
use derive_more::Display;
use gazebo::any::ProvidesStaticType;
use gazebo::cast;
use gazebo::prelude::*;
use serde::Serialize;
use serde::Serializer;
use starlark_map::small_map::SmallMap;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::collections::maybe_uninit_backport::maybe_uninit_write_slice;
use crate::collections::StarlarkHashValue;
use crate::collections::StarlarkHasher;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/values/layout/heap/call_enter_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use std::fmt::Debug;
use std::time::Instant;

use allocative::Allocative;
use gazebo::any::ProvidesStaticType;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::values::StarlarkValue;
use crate::values::Trace;
use crate::values::Value;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/values/layout/heap/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use std::ptr;

use dupe::Dupe;
use either::Either;
use gazebo::any::AnyLifetime;
use gazebo::cast;

use crate::any::AnyLifetime;
use crate::values::layout::avalue::AValue;
use crate::values::layout::heap::arena::MIN_ALLOC;
use crate::values::layout::heap::heap_type::HeapKind;
Expand Down
5 changes: 3 additions & 2 deletions starlark/src/values/layout/typed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ use gazebo::coerce::Coerce;
use gazebo::coerce::CoerceKey;
use serde::Serialize;

use crate::gazebo::any::AnyLifetime;
use crate::gazebo::any::ProvidesStaticType;
use crate as starlark;
use crate::any::AnyLifetime;
use crate::any::ProvidesStaticType;
use crate::values::alloc_value::AllocFrozenStringValue;
use crate::values::alloc_value::AllocStringValue;
use crate::values::int::PointerI32;
Expand Down
5 changes: 3 additions & 2 deletions starlark/src/values/layout/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ use dupe::Copy_;
use dupe::Dupe;
use dupe::Dupe_;
use either::Either;
use gazebo::any::AnyLifetime;
use gazebo::any::ProvidesStaticType;
use gazebo::cast;
use gazebo::coerce::coerce;
use gazebo::coerce::Coerce;
Expand All @@ -52,6 +50,9 @@ use serde::Serialize;
use serde::Serializer;
use starlark_map::Equivalent;

use crate as starlark;
use crate::any::AnyLifetime;
use crate::any::ProvidesStaticType;
use crate::collections::Hashed;
use crate::collections::StarlarkHashValue;
use crate::collections::StarlarkHasher;
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/values/layout/value_captured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ use std::cell::Cell;

use allocative::Allocative;
use derive_more::Display;
use gazebo::any::ProvidesStaticType;
use gazebo::prelude::*;

use crate as starlark;
use crate::any::ProvidesStaticType;
use crate::values::Freeze;
use crate::values::Freezer;
use crate::values::FrozenValue;
Expand Down
Loading

0 comments on commit c43b8a6

Please sign in to comment.