Connect with us

FACEBOOK

Rust Nibbles – Gazebo: AnyLifetime

Published

on

This article was written in collaboration with Neil Mitchell, a Software Engineer in the Developer Infrastructure organization at Facebook.

The Rust library Gazebo contains a collection of well-tested Rust utilities in the form of standalone modules. In this series of blog posts, we will cover some of the modules that make up the Gazebo library. In today’s blog, we will cover the trait AnyLifetime. This blog is a part of our Rust Nibbles series, where we go over the various Rust libraries we have open-sourced to learn more about what motivated their creation and how one can use them.

Rust provides the trait Any which serves as its helper for dynamic typing. Using the Any trait you can define:

fn print_if_string(arg: &dyn Any) { if let Some(string) = arg.downcast_ref::<String>() { println!("It's a string({}): '{}'", string.len(), string); } else { println!("Not a string..."); }
} 

Here, we are taking a value arg whose type is not statically known, then testing at runtime whether it is a String or not, and if it is, using it as a String. This code hasn’t turned Rust into a dynamically typed language, and there is nothing unsafe in our code. Under the hood, the Rust compiler generates a distinct TypeId for each type, and if the TypeId values match for any two values, their types are equivalent and Rust can safely convert between the two.

The Any trait works great, but it does impose a constraint that the type contained in the Any is ‘static. Which, in this context, means no lifetimes. In particular, we can’t use Any methods on a type such as:

Advertisement
free widgets for website
struct Value<'v> {...} 

This restriction was particularly problematic for our Starlark language implementation, where most types do indeed contain a ‘v lifetime argument. To work around this limitation, we defined AnyLifetime. Very similarly to Any, the AnyLifetime trait is defined in gazebo::any as:

pub unsafe trait AnyLifetime<'a>: 'a { fn static_type_id() -> TypeId where Self: Sized; fn static_type_of(&self) -> TypeId;
} 

Given a value (or just a type), we need to provide the TypeId which uniquely identifies this type. The only tweak here is that we define the trait to produce the TypeId of the equivalent static type, e.g. for Value<‘v> above we can use the TypeId of Value<‘static>:

unsafe impl<'v> AnyLifetime<'v> for Value<'v> { fn static_type_id() -> TypeId { TypeId::of::<Value<'static>>() } fn static_type_of(&self) -> TypeId { TypeId::of::<Value<'static>>() }
} 

Now we have access to the methods we know and love from Any, such as downcast_ref, but without the ‘static constraint. The only downside is that unsafe in the definition of the implementation. And it really is unsafe – if we claim that the TypeId of Value is that of String, we can convert between the two types at runtime, and things will go horribly wrong (with a segfault, most likely).

See also  How to Stream Vertical on Facebook Gaming

The solution is a series of increasingly powerful, but increasingly more error prone methods of defining instances. Starting with the simplest, we can define:

#[derive(AnyLifetime)]
struct Value<'v> {...} 

This works for types with no generic types and either zero or one lifetime parameters. Next, if we need to define the instance for any type, even type aliases, we can use the any_lifetime! macro:

Advertisement
free widgets for website
any_lifetime!(Value<'v>) 

And finally, if we need ultimate flexibility, we can define the instance head ourselves and just use a macro to define the body:

unsafe impl<'v> AnyLifetime<'v> for Value<'v> { any_lifetime_body!(Value<'static>);
} 

Of these, the final one is unsafe, as you are duty-bound to ensure the implementation type is the same as that in the body, but with static for all lifetime arguments.

The biggest problem with this approach is that while Any is built into the compiler and has a blanket implementation that supplies implementations for every type, AnyLifetime requires specific instances. Furthermore, it’s currently impossible to give an AnyLifetime implementation for Vec<T> given an AnyLifetime for T, because such instances don’t compose structurally (something we’ve failed to implement with a nice API, but may become possible with generic associated types). Despite these limitations, we’ve found AnyLifetime essential in the cases we’ve needed it.

We hope that this blog helps you understand the AnyLifetime trait, how to use it and gives you good insight into what it does. Look out for our next blog in this series, where we discuss the Comparisons in Gazebo, which provides utilities for operations such as comparison chaining.

See also  Some Facebook users say social media platform inaccessible in Myanmar

Be sure to check out our previous blogs in the Gazebo series to learn more about the various features the Gazebo library has to offer –
Gazebo – Prelude
Gazebo – Dupe
Gazebo – Variants

Advertisement
free widgets for website

About the Rust Nibbles series

We at Facebook believe that Rust is an outstanding language that shines in critical issues such as memory safety, performance and reliability. We joined the Rust Foundation to help contribute towards the growth, advancement and adoption of Rust, and towards sustainable development of open source technologies and developer communities across the world.

This blog is a part of our Rust Nibbles series, where we go over the various Rust libraries we have open-sourced to learn more about what motivated their creation and how one can use them. We hope that this series helps you create amazing projects by using these libraries and encourages you to try them out.

To learn more about Facebook Open Source, visit our open source site, subscribe to our YouTube channel, or follow us on Twitter and Facebook.

Facebook Developers

Advertisement
free widgets for website
Continue Reading
Advertisement free widgets for website

FACEBOOK

Introducing Facebook Graph API v18.0 and Marketing API v18.0

Published

on

By

introducing-facebook-graph-api-v180-and-marketing-api-v18.0

Today, we are releasing Facebook Graph API v18.0 and Marketing API v18.0. As part of this release, we are highlighting changes below that we believe are relevant to parts of our developer community. These changes include announcements, product updates, and notifications on deprecations that we believe are relevant to your application(s)’ integration with our platform.

For a complete list of all changes and their details, please visit our changelog.

General Updates

Consolidation of Audience Location Status Options for Location Targeting

As previously announced in May 2023, we have consolidated Audience Location Status to our current default option of “People living in or recently in this location” when choosing the type of audience to reach within their Location Targeting selections. This update reflects a consolidation of other previously available options and removal of our “People traveling in this location” option.

We are making this change as part of our ongoing efforts to deliver more value to businesses, simplify our ads system, and streamline our targeting options in order to increase performance efficiency and remove options that have low usage.

This update will apply to new or duplicated campaigns. Existing campaigns created prior to launch will not be entered in this new experience unless they are in draft mode or duplicated.

Advertisement
free widgets for website

Add “add_security_recommendation” and “code_expiration_minutes” to WA Message Templates API

Earlier this year, we released WhatsApp’s authentication solution which enabled creating and sending authentication templates with native buttons and preset authentication messages. With the release of Graph API v18, we’re making improvements to the retrieval of authentication templates, making the end-to-end authentication template process easier for BSPs and businesses.

With Graph API v18, BSPs and businesses can have better visibility into preset authentication message template content after creation. Specifically, payloads will return preset content configuration options, in addition to the text used by WhatsApp. This improvement can enable BSPs and businesses to build “edit” UIs for authentication templates that can be constructed on top of the API.

See also  Facebook group provides contacts, support for BC residents with livestock fleeing wildfires

Note that errors may occur when upgrading to Graph API v18 if BSPs or businesses are taking the entire response from the GET request and providing it back to the POST request to update templates. To resolve, the body/header/footer text fields should be dropped before passing back into the API.

Re-launching dev docs and changelogs for creating Call Ads

  • Facebook Reels Placement for Call Ads

    Meta is releasing the ability to deliver Call Ads through the Facebook Reels platform. Call ads allow users to call businesses in the moment of consideration when they view an ad, and help businesses drive more complex discussions with interested users. This is an opportunity for businesses to advertise with call ads based on peoples’ real-time behavior on Facebook. Under the Ad set Level within Ads Manager, businesses can choose to add “Facebook Reels” Under the Placements section.
  • Re-Launching Call Ads via API

    On September 12, 2023, we’re providing updated guidance on how to create Call Ads via the API. We are introducing documentation solely for Call Ads, so that 3P developers can more easily create Call Ads’ campaigns and know how to view insights about their ongoing call ad campaigns, including call-related metrics. In the future, we also plan to support Call Add-ons via our API platform. Developers should have access to the general permissions necessary to create general ads in order to create Call Ads via the API platform.

    Please refer to developer documentation for additional information.

Deprecations & Breaking Changes

Graph API changes for user granular permission feature

We are updating two graph API endpoints for WhatsAppBusinessAccount. These endpoints are as follows:

  • Retrieve message templates associated with WhatsAppBusiness Account
  • Retrieve phone numbers associated with WhatsAppBusiness Account

With v18, we are rolling out a new feature “user granular permission”. All existing users who are already added to WhatsAppBusinessAccount will be backfilled and will continue to have access (no impact).

The admin has the flexibility to change these permissions. If the admin changes the permission and removes access to view message templates or phone numbers for one of their users, that specific user will start getting an error message saying you do not have permission to view message templates or phone numbers on all versions v18 and older.

Advertisement
free widgets for website

Deprecate legacy metrics naming for IG Media and User Insights

Starting on September 12, Instagram will remove duplicative and legacy, insights metrics from the Instagram Graph API in order to share a single source of metrics to our developers.

This new upgrade reduces any confusion as well as increases the reliability and quality of our reporting.

After 90 days of this launch (i.e. December 11, 2023), we will remove all these duplicative and legacy insights metrics from the Instagram Graph API on all versions in order to be more consistent with the Instagram app.

We appreciate all the feedback that we’ve received from our developer community, and look forward to continuing to work together.

Please review the media insights and user insights developer documentation to learn more.

Advertisement
free widgets for website

Deprecate all Facebook Wi-Fi v1 and Facebook Wi-Fi v2 endpoints

Facebook Wi-Fi was designed to improve the experience of connecting to Wi-Fi hotspots at businesses. It allowed a merchant’s customers to get free Wi-Fi simply by checking in on Facebook. It also allowed merchants to control who could use their Wi-Fi and for how long, and integrated with ads to enable targeting to customers who had used the merchant’s Wi-Fi. This product was deprecated on June 12, 2023. As the partner notice period has ended, all endpoints used by Facebook Wi-Fi v1 and Facebook Wi-Fi v2 have been deprecated and removed.

API Version Deprecations:

As part of Facebook’s versioning schedule for Graph API and Marketing API, please note the upcoming deprecations:

Graph API

  • September 14, 2023: Graph API v11.0 will be deprecated and removed from the platform
  • February 8, 2024: Graph API v12.0 will be deprecated and removed from the platform
  • May 28, 2024: Graph API v13.0 will be deprecated and removed from the platform

Marketing API

  • September 20, 2023: Marketing API v14.0 will be deprecated and removed from the platform
  • September 20, 2023: Marketing API v15.0 will be deprecated and removed from the platform
  • February 06, 2024: Marketing API v16.0 will be deprecated and removed from the platform

To avoid disruption to your business, we recommend migrating all calls to the latest API version that launched today.

Facebook Platform SDK

As part of our 2-year deprecation schedule for Platform SDKs, please note the upcoming deprecations and sunsets:

  • October 2023: Facebook Platform SDK v11.0 or below will be sunset
  • February 2024: Facebook Platform SDK v12.0 or below will be sunset

First seen at developers.facebook.com

See also  How to Stream Vertical on Facebook Gaming
Continue Reading

FACEBOOK

Allowing Users to Promote Stories as Ads (via Marketing API)

Published

on

By

allowing-users-to-promote-stories-as-ads-(via-marketing-api)

Before today (August 28, 2023), advertisers could not promote images and/or videos used in Instagram Stories as ads via the Instagram Marketing API. This process created unwanted friction for our partners and their customers.

After consistently hearing about this pain point from our developer community, we have removed this unwanted friction for advertisers and now allow users to seamlessly promote their image and/or video media used in Instagram Stories as ads via the Instagram Marketing API as of August 28, 2023.

We appreciate all the feedback received from our developer community, and hope to continue improving your experience.

Please review the developer documentation to learn more.

First seen at developers.facebook.com

Advertisement
free widgets for website
See also  Facebook group provides contacts, support for BC residents with livestock fleeing wildfires
Continue Reading

FACEBOOK

Launching second release of Facebook Reels API: An enterprise solution for desktop and web publishers

Published

on

By

launching-second-release-of-facebook-reels-api:-an-enterprise-solution-for-desktop-and-web-publishers

We’re excited to announce that the second release of FB Reels API is now publicly available for third-party developers. FB Reels API enables users of third-party platforms to share Reels directly to public Facebook Pages and the New Pages Experience.

FB Reels API has grown significantly since the first release in September 2022. The new version of the APIs now support custom thumbnails, automatic music tagging, tagging collaborators, longer format of reels and better error handling.

FB Reels API will also support scheduling and draft capability to allow creators to take advantage of tools provided either by Meta or by our partners. Based on the feedback we received from our partners, we’ll now provide additional audio insights via the Audio Recommendations API and reels performance metrics via the Insights API.

Our goal in the next couple of releases is to continue to make it easier for creators to develop quality content by adding features like early copyright detection and A/B testing. We’re also excited to start working on enhanced creation features like Video clipping- so stay tuned to hear more about those features in the future.

Call-to-Action

If you are a developer interested in integrating with the Facebook Reels API, please refer to the Developer Documents for more info.

Advertisement
free widgets for website

Not sure if this product is for you? Check out our entire suite of sharing offerings.

Tune in to Product @scale event to learn more about FB Video APIs and hear from some of our customers.

First seen at developers.facebook.com

See also  Meta's Creative App Platform allows people to discover creative apps directly in Facebook Stories
Continue Reading

Trending